jQuery / Javascript String replace
jQuery / Javascript String replace
我想提供一些帮助。
javascript/jquery 中是否有某种方法可以像PHP 中的str_replace?
我的意思是,如果我有一个这样的字符串,例如:"l.123.aa-b"
我会将所有.
和-
替换为#
.
在 PHP 中,你会做这样的事情:
$a = l.123.aa-b;
$new_str = str_replace(array('-','.'),'#',$a);
任何帮助将不胜感激。谢谢。
只需调用'string'.replace()
var str = 'l.123.aa-b';
var newStr = str.replace('.', '#').replace('-', '#');
// or with a regex
var newStr = str.replace(/['.-]/g, '#'); // Thanks for the correction, Andbdrew
这可能
对你有用:
var str = 'l.123.aa-b';
var newStr = str.replace(/['.-]+/g, '#');
javascript 中有一个替换方法,类似于 php 中的 str_replace 方法。有关 w3schools 的更多信息 - http://w3schools.com/jsref/jsref_replace.asp
相关文章:
- Javascript 如何强制 string.replace 不将我的字符串解释为正则表达式
- 如何在javascript中使用string.replace.将匹配字符串开头的两个不同表达式替换为null
- JavaScript String.replace()中的错误,替换字符串“”$'&”;
- string.replace(“é”, “e”) 不起作用
- string.replace(fromCharCode() , '') 不能替换字符
- Javascript - string.replace() not replacing ALL
- 使用 string.replace() 更改 URL
- Javascript 中的 String.replace() 方法可以用来添加换行吗?
- 从回调函数中向 String.replace 返回一个值
- JavaScript String.replace:如何知道上次调用回调函数
- 如何使用 String.replace() 方法而不弄乱 MS Word 2013 中的空白段落和对象标识符
- jQuery / Javascript String replace
- 为IE8优化JS-String.replace和String.match
- `string.replace`使用美元符号($)作为替换时的怪异行为
- 正在转义String.replace的$符号
- String.replace IE11中的奇怪行为
- String.replace(Regex) returns undefineds
- 将string.replace()Javascript与Unicode符号一起使用时出现问题
- 什么是“;if(!''.replace(/^/,String))“;做
- 使用正则表达式的javascript中的String.replace方法