解决js文件替换字符,只能替换一个字符

1,只替换一次:

<script type="text/javascript"> 


var str="Visit Microsoft!" 
document.write(str.replace(/Microsoft/, "W3School")) 

</script> 


2,全局替换(正则替换加参数g)


<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, "W3School")) 

</script> 


备注 :/m参数是多行匹配

猜你喜欢

转载自blog.csdn.net/fuweihua123/article/details/78953689