JavaScript的正則表達式正則表達式
1.字面量數組
(1)ide
let reg = /\bis\b/; let test = 'She is a beautiful girl.He is a handsome boy.You are a dog.'; layer.alert(test.replace(reg, 'IS'));
結果:函數
She IS a beautiful girl.He is a handsome boy.You are a dog.
(2) 這裏只替換了第一個is,若是要全局替換,在後面加個gcode
let reg = /\bis\b/g; let test = 'She is a beautiful girl.He is a handsome boy.You are a dog.'; layer.alert(test.replace(reg, 'IS'));
結果:regexp
She IS a beautiful girl.He IS a handsome boy.You are a dog.
2.構造函數視頻
(1)對象
let reg = new RegExp('\\bis\\b'); let test = 'She is a beautiful girl.He is a handsome boy.You are a dog.'; layer.alert(test.replace(reg, 'IS'));
結果:索引
She IS a beautiful girl.He is a handsome boy.You are a dog.
(2) 若是要全局替換,在後面加個gip
let reg = new RegExp('\\bis\\b', 'g'); let test = 'She is a beautiful girl.He is a handsome boy.You are a dog.'; layer.alert(test.replace(reg, 'IS'));
結果:
She IS a beautiful girl.He IS a handsome boy.You are a dog.
匹配符:
1.將abc中任意字符替換成M
let reg = /[abc]/g; let test = '1a2b3c4d'; layer.alert(test.replace(reg, 'M'));
結果:
1M2M3M4d
2.取反,除了abc之外的全部字符都替換成M
let reg = /[abc]/g; let test = '1a2b3c4d'; layer.alert(test.replace(reg, 'M'));
結果:
MaMbMcMM
let reg = /[a-zA-Z]/g; let test = '1a2b3c4d5A6F7X8Z'; layer.alert(test.replace(reg, 'M'));
結果:
1M2M3M4M5M6M7M8M
邊界匹配字符:
1.貪婪模式(儘量多的匹配)
let reg = /\d{3,6}/g; let test = '12345678'; layer.alert(test.replace(reg, 'X'));
結果:
X78
2.非貪婪模式(儘量少的匹配)
let reg = /\d{3,6}?/g; let test = '12345678'; layer.alert(test.replace(reg, 'X'));
結果:
XX78
1.匹配數字加小寫字母連續出現3次的場景
let reg = /(\d[a-z]){3}/g; let test = '1a2b3c4d'; layer.alert(test.replace(reg, 'X'));
結果:
X4d
2.或
let reg = /1a2b3c(4d|5e)6f7g8h/g; let test = '1a2b3c4d6f7g8h 1a2b3c5e6f7g8h'; layer.alert(test.replace(reg, 'X'));
結果:
X X
3.反向引用(捕獲分組中內容)
let reg = /(\d{4})-(\d{2})-(\d{2})/g; let test = '2019-10-30'; layer.alert(test.replace(reg, '$2/$3/$1'));
結果:
10/30/2019
注:
let reg = /(?:\d{4})-(\d{2})-(\d{2})/g; //忽略第一個分組
例1.
let reg = /\w(?=\d)/g; let test = 'a1b2c^d$'; layer.alert(test.replace(reg, 'X'));
結果:
X1X2c^d$
例2.
let reg = /\w(?!\d)/g; let test = 'a1b2c^d$'; layer.alert(test.replace(reg, 'X'));
結果:
aXbXX^X$
js無後顧!!!
例:
let reg = /\w/g; layer.alert(reg.global+"</br>"+ reg.ignoreCase+"</br>"+ reg.multiline+"</br>"+ reg.lastIndex+"</br>"+ reg.source);
結果:
true false false 0 \w
1.test方法,找到當前匹配位置的索引(全局調用)
let reg2 = /\w/g; let result = ''; while(reg2.test('abcdef')){ result += reg2.lastIndex + '</br>'; } layer.alert(result);
結果:
1 2 3 4 5 6
2.exec方法
若是沒有匹配的文本則返回null,不然返回一個結果數組
(1).非全局調用
例.
let reg1 = /\d(\w)\d/; let ts = '1a2b3c4d5e'; let ret = reg1.exec(ts); let result = ''; result += reg1.lastIndex + ' ' + ret.index + ' ' + ret; result += '</br>'; result += reg1.lastIndex + ' ' + ret.index + ' ' + ret; layer.alert(result);
結果:
0 0 1a2,a 0 0 1a2,a
(2).全局調用
例.
let reg2 = /\d(\w)\d/g; let ts = '1a2b3c4d5e'; let ret = ''; let result = ''; while(ret = reg2.exec(ts)){ result += reg2.lastIndex + ' ' + ret.index + ' ' + ret; result += '</br>'; } layer.alert(result);
結果:
3 0 1a2,a 7 4 3c4,c
1.search()方法
2.match()方法
(1).非全局調用
例:
let reg1 = /\d(\w)\d/; let ts = '1a2b3c4d5e'; let ret = ts.match(reg1); let result = ''; result += ret + ' ' + ret.index + ' ' + reg1.lastIndex; layer.alert(result);
結果:
1a2,a 0 0
(2).全局調用
例:
let reg2 = /\d(\w)\d/g; let ts = '1a2b3c4d5e'; let ret = ts.match(reg2); let result = ''; result += ret + ' ' + ret.index + ' ' + reg2.lastIndex; layer.alert(result);
結果:
1a2,3c4 undefined 0
3.split()方法
將字符串分割爲字符數組
例:
let reg1 = /\d/g; let ts = '1a2b3c4d5e'; let ret = ts.split(reg1); console.log(ret) //layer不支持數組的彈出,因此改成console
結果:
["", "a", "b", "c", "d", "e"]
4.replace()方法
例1:
let reg1 = /1/g; let ts = '1a1b1c1d1e'; let result = ts.replace(reg1, 2); layer.alert(result);
結果:
2a2b2c2d2e
replace第二個參數也能夠爲一個function
function會在每次匹配替換的時候調用,有四個參數
(1).匹配字符串
(2).正則表達式分組內容,沒有分組則沒有該參數
(3).匹配項在字符串中的index
(4).原字符串
例2:沒有分組
let reg1 = /\d/g; let ts = '1a2b3c4d5e'; let ret = ts.replace(reg1, function(match, index, origin){ console.log(index); return parseInt(match)+1; }); console.log(ret);
結果:
0 2 4 6 8 2a3b4c5d6e
例3:有分組(注意參數有變化)
let reg1 = /(\d)(\w)(\d)/g; let ts = '1a2b3c4d5e'; let ret = ts.replace(reg1, function(match, group1, group2, group3, origin){ console.log(match); return group1 + group3; }); console.log(ret);
結果:
1a2 3c4 12b34d5e