Javascript中使用正则表达式
Javascript中使用正则表达式:
在Javascript里,有两种使用正则的方法,一是创建一个正则表达式的实例,而是使用String对象里的正则表达相关的方法.
首先是正则表达式的创建,有2种办法:
var my_regex=/[a-z]+/g;
var my_regex=new ("[a-z]+","g");
方法
exec(string),对string进行正则处理,并返回匹配结果.
test(string),测试string是否含有匹配结果
字符串对象中的正则
方法
match(pattern) 根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
search(pattern) 根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
replace(pattern,replacement) 根据pattern进行正则匹配,把匹配结果替换为replacement
split(pattern) 根据pattern进行正则分割,返回一个分割的数组。
在Javascript里,有两种使用正则的方法,一是创建一个正则表达式的实例,而是使用String对象里的正则表达相关的方法.
首先是正则表达式的创建,有2种办法:
var my_regex=/[a-z]+/g;
var my_regex=new ("[a-z]+","g");
方法
exec(string),对string进行正则处理,并返回匹配结果.
test(string),测试string是否含有匹配结果
字符串对象中的正则
方法
match(pattern) 根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
search(pattern) 根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
replace(pattern,replacement) 根据pattern进行正则匹配,把匹配结果替换为replacement
split(pattern) 根据pattern进行正则分割,返回一个分割的数组。
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号