javascript中的replaceAll功能

来源:互联网 发布:手机淘宝评价怎么改 编辑:程序博客网 时间:2024/05/11 13:17

原创  javascript中的replaceAll功能。 

请注意/Microsoft/g,就是这个小g

在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3C":

<script type="text/javascript">

var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."

document.write(str.replace(/Microsoft/g, "W3C"))

</script>

输出:

Welcome to W3C! We are proud to announce that W3C
has one of the largest Web Developers sites in the world.