JavaScirpt基础 正则表达式 replace() 方法使用字符串
replace() 方法使用字符串
举例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>replace() 方法使用字符串</title>
</head>
<body>
<p>替换 "Microsoft" 为 "Runoob" :</p>
<button onclick="myFunction()">点我</button>
<p id="demo">请访问 Microsoft!</p>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","Runoob");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
替换 "Microsoft" 为 "Runoob" :
请访问 Microsoft!
请访问 Runoob!