<!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 text = document.getElementById("demo").innerHTML; let result = text.replace(/blue|house|car/gi, function (x) { return x.toUpperCase(); }); document.getElementById("demo").innerHTML = result; </script> </body> </html>