JavaScript: RegExp + replace

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

This picture shows the result after we exchange:

JavaScript: RegExp + replace

Below is this project's code:

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>RegExp</title>
</head>
<script>
function strreplace(){
var str=document.getElementById("str").value;
var newstr=str.replace(/paul/g,"Ringo");
document.getElementById("demo").innerHTML = newstr;
}
</script>
<body>
请输入一串带有“paul”(小写)的单词串,不同单词用<br>空格隔开,点击“替换”,可将“paul”替换成“Ringo”.
<br><br><br>你可以复制这段字符进下框测试:paul like Spaul Paul paul
<br><br>会发现所有的“paul”都被替换成“Ringo”。
<br><br><br><input type="text" name="str" id="str">
<input type="button" value="替换" onclick="strreplace()">
<br><br>替换后:<p id="demo"></p>
</body>
</html>
上一篇:@ControllerAdvice详解


下一篇:适应各浏览器图片裁剪无刷新上传jQuery插件(转)