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>

不求与人相比,但求超越自己,要哭就哭出激动的泪水,要笑就笑出成长的性格。

javascript compile()方法

相关文章:

你感兴趣的文章:

标签云: