JavaScript 中repalce的使用
把Paul Pauline pual Paula Paul中的Paul替换成Ringo
1 <!DOCTYPE html> 2 <html> 3 4 <body> 5 6 7 <button onclick="myFunction()">Try it</button> 8 9 <p id="demo">Paul,Paula,paul,Pauline,Paul</p> 10 11 <script> 12 function myFunction() { 13 var str = document.getElementById("demo").innerHTML; 14 var txt = str.replace(/\bPaul\b/g,"Ringo"); 15 document.getElementById("demo").innerHTML = txt; 16 } 17 </script> 18 19 </body> 20 </html>