【发布时间】:2014-10-22 05:50:00
【问题描述】:
text = "1/2/3"
result = text.replace("/", "");
我希望结果是“123”,但结果却是“12/3” 为什么?
【问题讨论】:
-
@Kamafeather Thx,刚刚看了,正是我需要的
-
投票结束我自己的问题,因为它确实是重复的
标签: javascript replace
text = "1/2/3"
result = text.replace("/", "");
我希望结果是“123”,但结果却是“12/3” 为什么?
【问题讨论】:
标签: javascript replace
添加全局选择“g”标志并在第一个参数中使用正则表达式而不是字符串。
result = text.replace(/\//g, "");
【讨论】:
您可以使用正则表达式作为参数来替换全局选择。
"1/2/3".replace(/\//g, "")
【讨论】:
你可以试试下面的正则表达式
"1/2/3".replace(/\//g,"");
这会将所有/ 元素替换为""。
【讨论】:
另一种方法:
String.prototype.replaceAll = function(matchStr, replaceStr) {
return this.replace(new RegExp(matchStr, 'g'), replaceStr);
};
var str = "1/2/3";
result = str.replaceAll('/', '');
【讨论】:
a = "1/2/3"
a =a.split("/").join("")
【讨论】: