輕鬆了解正規表示式的幾個屬性和方法

2021-09-25 16:11:29 字數 4188 閱讀 6152

一、我們每個js中的regexp物件都有6個屬性:

1.flags

2.global

3.ignorecase

4.multiline

5.source

6.lastindex

其中1-5屬性為唯讀,6屬性為可讀寫

(1)flags

返回乙個字串,裡面包含著正規表示式中所有的修飾符

const regex = /hello world/igm;

console.log(regex.flags);

//列印 gim

(2)global

返回乙個布林值,表示是否設定了全域性匹配修飾符(g)。

const regex = /hello world/g;

console.log(regex.global);

//列印 true

const regex1 = /hello world/;

console.log(regex1.global);

//列印 false

const str1 = "hello world,hello world";

console.log(str1.match(regex1));

//列印 ["hello world"]

console.log(str1.match(regex));

//列印 ["hello world","hello world"]

(3)ignorecase

返回乙個布林值,表示是否設定了不區分大小寫修飾符(i)

const regex = /hello world/i;

console.log(regex.ignorecase);

//列印 true

const regex1 = /hello world/;

console.log(regex1.ignorecase);

//列印 false

const str1 = "hello world";

console.log(str1.match(regex1));

//列印 null

console.log(str1.match(regex));

//列印 ["hello world"]

(4)multiline

返回乙個布林值,表示是否設定了多行模式中執行匹配修飾符(m)

const regex = /hello world/m;

console.log(regex.multiline);

//列印 true

const regex1 = /hello world/;

console.log(regex1.multiline);

//列印 false

const str1 = "hello\nhello world";

console.log(str1.match(regex1));

//列印 null

console.log(str1.match(regex));

//列印 ["hello world"]

(5)source

返回正規表示式的字串形式(不包括反斜槓)

const regex = /hello world/;

console.log(regex.source;

//列印 hello world

(6)lastindex

返回乙個整數,表示下一次開始搜尋的位置。該屬性可讀寫,但是只在進行連續搜尋時有意義(即全域性搜尋 : g),這個屬性一般被正規表示式的test()和exec()方法用到。

到這裡我們先來了解一下test()和exec()方法

二、正規表示式的兩個方法

1.test()

正則例項物件的test方法返回乙個布林值,表示當前模式是否能匹配引數字串。

const regex = /hello world/;

const regex1 = /hello my world/;

const str1 = "hello world";

console.log(regex.test(str1));

//列印 true

console.log(regex1.test(str1));

//列印 false

2.exec()

正則例項物件的exec方法,用來返回匹配結果。如果發現匹配,就返回乙個陣列,成員是匹配成功的子字串,否則返回null。

const regex = /hello world/;

const regex1 = /hello my world/;

const str1 = "hello world";

console.log(regex.exec(str1));

//列印 ["hello world"]

console.log(regex1.exec(str1));

//列印 null

好的,這個時候我們回去看lastindex屬性,首先我們要知道如果正規表示式帶有g修飾符,則每一次test方法和exec方法都從上一次結束的位置開始向後匹配。

什麼意思呢?

const regex = /h/g;

const str1 = "hhhhhhhhhhhhhhh";

console.log(regex.exec(str1));

//列印 ["h",index:0]

console.log(regex.lastindex);

//列印 1

console.log(regex.exec(str1));

//列印 ["h",index:1]

console.log(regex.lastindex);

//列印 2

index是當前滿足要求的字串的起始索引值,lastindex是當前滿足要求的字串的結束索引值,也是全域性搜尋下一次檢索時候的起始索引值。

這裡便產生了乙個問題:

const regex = /\d/g;

const str1 = "h2";

console.log(regex.test(str1));

//true

console.log(regex.lastindex);

//2console.log(regex.test(str1));

//false

console.log(regex.lastindex);

//0

這裡因為全域性匹配,每次呼叫test()都會更新lastindex的值,改變了檢索的起始位置所以第二次沒有匹配到,返回了false,同時我們還注意到返回false之後lastindex的值變為了0,當呼叫test( )的正規表示式物件具有修飾符g時,它將把當前正規表示式物件的lastindex屬性設定為緊挨著匹配子串的字元位置,當同乙個正規表示式第二次呼叫test( ),它會將從lastindex屬性所指示的字串處開始檢索,如test( )沒有發現任何匹配結果,它會將lastindex重置為0。(exec方法同上)

我們之前說過lastindex是可讀可寫的,那麼我們可以手動修改它的值,那麼前面那段**第二次執行就不會返回false。

const regex = /\d/g;

const str1 = "h2";

console.log(regex.test(str1));

//true

console.log(regex.lastindex);

//2regex.lastindex = 1;

console.log(regex.lastindex);

//1console.log(regex.test(str1));

//true

console.log(regex.lastindex);

//2

了解正規表示式

摘要 相信大家都聽說過正規表示式,用它可以在字段中進行高速查詢 替換等功能。使用正規表示式可以輕鬆的對文字進行控制。比如,如果您想知道乙個字串的數字表示式是否在 0.1 9.9 之間 注意,只有一位小數 您會用什麼方法辦到這個需求呢?我想您會乙個 char 乙個char 的檢測,這樣會很麻煩,需要很...

了解正規表示式

元字元 表示的是 或者 表示的是 分組 提公升優先順序 都是元字元,下面這些也可以叫限定符 限定前面的表示式出現的次數 表示的是 前的表示式出現了0次到1次,最少是0次,最多1次,另乙個含義 阻止貪婪模式 表示的是 更加明確前面的表示式出現的次數 表示的是以 開始,或者是取非 取反 表示的是以 結束...

了解正規表示式

正規表示式 regular expression 是乙個描述字元模式的物件。正規表示式能夠進行強大的 模式匹配 和 文字檢索與替換 功能。前端往往有大量的表單資料校驗的工作,採用正規表示式會使得資料校驗的工作量大大減輕 建立正規表示式 regexp建構函式宣告 第乙個引數就是我們的模式 字串 var...