javascript中关于字符串替换 replace() 方法的使用
替换一次
function ReplaceDemo(){
var ss = "The+man+hit+the+ball+with+the+bat.\n";
alert(ss) ;
var re = /\+/;
var r = ss.replace(re, " ");
alert(r) ;
}
打印:The+man+hit+the+ball+with+the+bat.
The man+hit+the+ball+with+the+bat.
全部替换
function ReplaceDemo(){
var ss = "The+man+hit+the+ball+with+the+bat.\n";
alert(ss) ;
var re = /\+/g;
var r = ss.replace(re, " ");
alert(r) ;
}
打印:The+man+hit+the+ball+with+the+bat.
The man hit the ball with the bat.
本文介绍JavaScript中字符串替换方法replace()的使用技巧,包括如何替换字符串中的特定字符一次及全部替换的方法,并通过具体示例展示了不同正则表达式的应用。

被折叠的 条评论
为什么被折叠?



