正则表达式替换

 

代码如下:

<!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/g,"Ringo");
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

posted @ 2018-11-11 20:21  冬一君  阅读(132)  评论(0编辑  收藏  举报