javascript compile()步骤

javascript compile()方法

用法
RegExpObject.compile(regexp,modifier)
用于在脚本执行过程中编译正则表达式,也可用于改变和重新编译正则表达式。
regexp为正则表达式
modifier为规定匹配的类型。"g" 用于全局匹配,"i" 用于区分大小写,"gi" 用于全局区分大小写的匹配。

例子:
在字符串中全局搜索 "today",并用 "tommorow" 替换。
然后通过 compile() 方法,改变正则表达式,用 "boat" 替换 "rld" 或 "world",:

<html>
<body>

<script type="text/javascript">

var str="hello world,today is a good day!";
patt=/today/g;
str2=str.replace(patt,"tommorow");
document.write(str2+"<br />");
patt=/(wo)?rld/g;
patt.compile(patt); 
str2=str.replace(patt,"boat");
document.write(str2);

</script>

</body>
</html>