JavaScript: RegExp + replace


 

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

 

This picture shows the result after we exchange:

 

Below is this project's code:

 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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM