javascript(replace())

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

str.replace(regexp/substr,replacement)

返回值

一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。

全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School":
(str.replace(/Microsoft/g, "W3School"))

把 "Doe, John" 转换为 "John Doe" 的形式:
name = "Doe, John";
name.replace(/(\w+)\s, \s(\w+)/, "$2 $1");

把字符串中所有单词的首字母都转换为大写
name = 'aaa bbb ccc';
uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
);
return word.slice(0,1).toUpperCase()+word.slice(1).toLowerCase();

你可能感兴趣的:(javascript(replace()))