检索表达式然后替换它
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 正则表达式</h1>
- <p>将“microsoft”替换为以下段落中的“baidu”:</p>
- <button onclick="myFunction()">试一试</button>
- <p id="demo">Please visit Microsoft and Microsoft!</p>
- <script>
- function myFunction() {
- var str = document.getElementById("demo").innerHTML;
- var txt = str.replace(/microsoft/i,"baidu");
- document.getElementById("demo").innerHTML = txt;
- }
- </script>
- </body>
- </html>