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)

相關文章
相關標籤/搜索