代码实现如下:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p>正则表达式把所有Paul替换成Ringo:Paul Puala Pualine paul Paul
</p>
<button onclick="myFunction()">疯狂点我</button>
<p id="demo">Paul Puala Pualine paul Paul!</p>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/Paul/m,"Ringo");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
结果如下:
原文地址:https://www.cnblogs.com/mumu597/p/9855802.html
时间: 2024-10-16 09:42:13