js正则表达式的match test exec replace函数

js正则表达式的方法:一种正则在前,一种正则在后:html

使用:正则表达式

1.exec数组

 var res = /\-[a-z]/g .exec("font-size");spa

console.log(res);regexp

获得的结果:htm

因此返回的是一个数组,第一个为匹配值,第二个是匹配的位置,第三个是输入的数blog

2.test索引

var res = /\-[a-z]/g .test("font-size");
console.log(res);io

返回为一个布尔值console

3.match

var res =("font-size-style").match( /\-[a-z]/g );
console.log(res);

 

返回为匹配到的值

4.replace

("font-size-style").replace( /\-[a-z]/g ,function(a,b,c){
console.log(a);
console.log(b);
console.log(c);
});

返回的a为匹配值,b为索引值,C为输入值;当有多个值的时候,如上图,是一组循环,这种很是适合匹配多值

replace经常使用来匹配全局,匹配首字母

 

 

 

 

 

 

 

 

 

参考连接:https://www.w3cschool.cn/regexp/m2ez1pqk.html  (W3Cschool)

相关文章
相关标签/搜索