JavaScript: RegExp + replace

 

We can use RegExp + replace to change Specific text into others we want.html

 

This picture shows the result after we exchange:測試

 

Below is this project's code:this

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4   <meta charset="UTF-8">
 5   <title>RegExp</title>
 6 </head>
 7 <script>
 8     function strreplace(){
 9         var str=document.getElementById("str").value;
10         var newstr=str.replace(/paul/g,"Ringo");
11         document.getElementById("demo").innerHTML = newstr;
12     }
13 </script>
14 <body>
15     請輸入一串帶有「paul」(小寫)的單詞串,不一樣單詞用<br>空格隔開,點擊「替換」,可將「paul」替換成「Ringo」.
16     <br><br><br>你能夠複製這段字符進下框測試:paul like Spaul Paul paul
17     <br><br>會發現全部的「paul」都被替換成「Ringo」。
18     <br><br><br><input type="text" name="str" id="str">
19     <input type="button" value="替換" onclick="strreplace()">
20     <br><br>替換後:<p id="demo"></p>
21 </body>
22 </html>
相關文章
相關標籤/搜索