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.