①替换指定的
var str="abcda Microsoft!" str.replace(/a/, "x"); //将第一个a替换为x 结果为 xbcda Microsoft
②替换字符串中
var str="abcda Microsoft!" str.replace(/a/g, "x"); //将所有a替换为x 结果为 xbcdx Microsoft
③首字母转大写
<script type="text/javascript">
name = ‘aaa bbb ccc‘;
uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
);
document.write (uw);
</script>本文出自 “JianBo” 博客,转载请与作者联系!
原文:http://jianboli.blog.51cto.com/12075002/1950508