本文實例講述了JavaScript使用replace函數替換字符串的方法。分享給大家供大家參考。具體如下:
JavaScript通過replace函數替換字符串,下面的代碼將Visit Microsoft中的MicroSoft替換成poluoluo.com
<!DOCTYPE html> <html> <body> <p> Click the button to replace "Microsoft" with "poluoluo.com" 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","poluoluo.com"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
希望本文所述對大家的javascript程序設計有所幫助。