js的正则表达式的替换

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
var str='Paul,Paula,paul,Ppaul,Paul';//字符串
var result=str.replace(/\bPaul\b/g,"ringo");//将字符串中的Paul单词替换成ringo
document.write(result);
</script>

或者

<script>
var str='Paul,Paula,paul,Ppaul,Paul';//字符串

var substr=new RegExp(/\bPaul\b/,"g")
var result=str.replace(substr,"ringo");//将字符串中的Paul单词替换成ringo
document.write(result);
</script>
</body>
</html>

运行结果为:ringo,Paula,paul,Ppaul,ringo

posted @ 2018-10-26 14:39  山茶花llia  阅读(2242)  评论(0编辑  收藏  举报