組和范圍表示表達(dá)字符的組和范圍。
// x|y 匹配x或y
console.log(('chrisfang212').match(/\w|\d/ig)) //[ 'c', 'h', 'r', 'i', 's', 'f', 'a', 'n', 'g', '2', '1', '2' ]
// [xyz] [x-z] 字符集, 表示匹配任何一個封閉的字符
console.log(('chrisfang212').match(/[abcdefg]/ig)) // [ 'c', 'f', 'a', 'g' ]
console.log(('chrisfang212').match(/[a-g]/ig)) // [ 'c', 'f', 'a', 'g' ]
// [^xyz] [^x-z] 否定字符集, 表示匹配不在范圍的任何一個封閉的字符
console.log(('chrisfang212').match(/[^abcdefg]/ig)) // [ 'h', 'r', 'i', 's', 'n', '2', '1', '2' ]
console.log(('chrisfang212').match(/[^a-g]/ig)) // [ 'h', 'r', 'i', 's', 'n', '2', '1', '2' ]
// (x) 匹配x,并且記住x
console.log(('chrisfang212').replace(/\w+(fang212)/, 'qiang$1')) //qiangfang212
// \n (n是一個正整數(shù)) \n代表著第幾個括號所捕獲的值 而非 表達(dá)式
console.log(('chrisfang212').match(/(\d)\1/)) // null
console.log(('chrisfang222').match(/(\d)\1/)) // 22
// (?<Name>x) 匹配x并將其以<Name>指定的名稱存儲在返回的匹配項的groups屬性中丑蛤。組名必須使用尖括號(“ <”和“>”)
console.log(('chrisfang222').match(/(?<hello>\d)/).groups.hello) //2
// (?:x) 非捕獲組:匹配x但不記得匹配項万矾。
console.log(('chrisfang222').match(/(?:\d)/)) // 2
console.log(('chrisfang222').match(/(?:\d)\1/)) // null
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者