本文操作环境:windows7系统、javascript1.8.5版,dell g3电脑。
comp是javascript的方法吗?
comp不是javascript的方法,而compile()是javascript的方法。
javascript compile() 方法
compile() 方法用于在脚本执行过程中编译正则表达式。
compile() 方法也可用于改变和重新编译正则表达式。
语法
regexpobject.compile(regexp,modifier)
参数 regexp 正则表达式。
modifier 规定匹配的类型。"g" 用于全局匹配,"i" 用于区分大小写,"gi" 用于全局区分大小写的匹配。
注:除了 opera 浏览器外,其他浏览器都支持 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>
以上实例输出结果:
every person in the world! every woperson on earth!every person in the world! every person on earth!
推荐学习:《javascript基础教程》
以上就是comp是javascript的方法吗的详细内容。
