<!DOCTYPE html> <html> <body> <h1>JavaScript 字符串</h1> <h2>replace() 方法</h2> <p>replace() 方法在字符串中搜索值或正则表达式,并返回已替换值的新字符串。</p> <p id="demo">Mr Blue has a blue house and a blue car.</p> <script> let str = document.getElementById("demo").innerHTML; let res = str.replace(/blue/g, "red"); document.getElementById("demo").innerHTML = res; </script> </body> </html>