代码示例:(标识:eg_js_regexp_string_replace)
<!DOCTYPE html>
<html>
<body>

<h1>JavaScript 正则表达式</h1>

<p>将“microsoft”替换为以下段落中的“(www.sou-xun.com)”:</p>

<button onclick="myFunction()">试一试</button>

<p id="demo">Please visit Microsoft and Microsoft!</p>

<script>
function myFunction() {
  var str = document.getElementById("demo").innerHTML; 
  var txt = str.replace(/microsoft/i,"(www.sou-xun.com)");
  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>
运行结果: