JavaScript 参考手册
JavaScript compile() 方法
在字符串中全局搜索 "man",并用 "person" 替换。然后通过 compile() 方法,改变正则表达式,用 "person" 替换 "man" 或 "woman",
源代码:
点击运行 »
<script> var str = "Every man in the world! Every woman on earth!"; var patt = /man/g; var str2 = str.replace(patt, "person"); document.write(str2 + "<br>"); patt = /(wo)?man/g; patt.compile(patt); str2 = str.replace(patt, "person"); document.write(str2); </script>
运行结果:
点击运行 »