JavaScript Replace()方法
JavaScript replace()方法使用正则表达式工作。
Replace()方法用指定的值搜索并用新的给定值替换它。
语法
string.replace(oldvalue, newvalue)
JavaScript Replace()方法示例
在这个例子中, 将hello替换成welcome
<html> <head> <script type="text/javascript"> function Myfun() { var str = document.getElementById("rep").innerHTML; var st = str.replace("Hello", "Welcome"); document.getElementById("rep").innerHTML = st; } </script> </head> <body> <p>js 替换字符串</p> <h5 id="rep"> Hello</h5> <button onclick="myfun()">点击我</button> </body> </html>
日期:2020-04-18 01:09:25 来源:oir作者:oir