您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

JavaScript使用replace函数替换字符串的方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 本文实例讲述了JavaScript使用replace函数替换字符串的方法。分享给大家供大家参考。具体如下:

JavaScript通过replace函数替换字符串,下面的代码将Visit Microsoft中的MicroSoft替换成jb51.net

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html> <html> <body> <p> Click the button to replace "Microsoft" with "jb51.net" in the paragraph below: </p> <p id="demo">Visit Microsoft!</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","jb51.net"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>