正則的擴充套件

2022-05-17 18:02:30 字數 2092 閱讀 9793

es5中有兩種情況

let regex = new regexp('xyz', 'i')

// 等價於

let regex = /xyz/i

let regex = new regexp(/xyz/i)

// 等價於

let regex = /xyz/i

注意!!!

let regex = new regexp(/xyz/, 'i')

// 這種寫法是錯誤的

es6的改變
new regexp(/abc/ig, 'i').flags

// 第二個引數i會將前面的ig進行覆蓋

let str="1 plus 2 equal 3"

str.match(/\d+/g)

// ['1','2','3']

let str = 'nihao jack'

str.replace(/jack/, 'lucy')

// nihao lucy

let str = 'good body'

str.search(/body/)

// 5

let str = 'good body'

str.search(/girl/)

// -1

let str = 'good body'

str.split('o')

["g", "", "d b", "dy"]

let str = /hello/;

let str2 = /hello/u;

str.unicode // false

str2.unicode // true

let str = 'aaa_aa_a'

let reg1 = /a+/g

let reg2 = /a+/y

reg1.exec(s) // ['aaa']

reg2.exec(s) // ['aaa']

reg1.exec(s) // ['aa']

reg2.exec(s) // null y修飾符從剩餘項的第乙個位置開始(即_)所以找不到

lastindex屬性可以指定每次搜尋的開始位置

reg2.lastsindex = 1

reg2.exec(s) // ['aa']

實際上y修飾符號隱含了頭部匹配的標誌^

'a1a2a3'.match(/a\d/y) // ['a1']

'a1a2a3'.match(/a\d/gy) // ['a1','a2','a3']

const reg = /(\d)-(\d)-(\d)/

const matchobj = reg.exec('1999-12-31')

const year = matchobj[1]; // 1999

const month = matchobj[2]; // 12

const day = matchobj[3]; // 31

問題: 只能用數字序號引用,組的順序改變,引用的時候就必須修改序號

const reg = /(?\d)-(?\d)-(?\d)/

const matchobj = reg.exec('1999-12-31')

const year = matchobj.groups.year // 1999

const month = matchobj.groups.month // 12

const day = matchobj.groups.day // 31

如果具名組沒有匹配,那麼對應的groups物件屬性會是undefined

let } = /^(?.*):(?.*)$/u.exec('foo:bar')

console.log() //

Linux 正則 擴充套件正則

基礎正規表示式 以什麼什麼開頭 m 以什麼什麼結尾 m 還表示空行,或空格,可以用cat an 試一下 空行 什麼符號都沒有 表示任意 乙個字元 轉義字元不解析特殊符號的含義 n 相當於回車鍵 t 相當於tab鍵 表示前乙個字元連續出現了0次或0次以上 表示任意字元,包括空行,正規表示式表示所有或連...

正則和擴充套件正則的歸類總結

元字元 在正規表示式中有特殊意義的專用字元,如點 星 等 前導字元 元字元前一位的字元 緊挨著,前一位 1 任意單個字元,除了換行符 2 前導字元出現0次或者連續出現多次 含一次 3 任意長度的字元 ab.ab888 ab abc abcc abb 4 行的開頭 grep g 1.txt 5 行的結...

Form中正則的擴充套件

1.form元件擴充套件 1.簡單擴充套件 利用form元件自帶的正則擴充套件 a.方式一 from django.forms import form from django.forms import widgets from django.forms import fields from djan...