js 正则

239 阅读2分钟

1,正则

RegExp 对象

有两种方法实例化 RegExp 对象:

  • 字面量
/正则表达式主体/修饰符(可选)

var patt = /runoob/i
  • 构造函数
new RegExp('正则表达式主体','修饰符(可选)')

var reg = new RegExp('\\bis\\b','g');

特殊字符:

*,+,?,$,^,.,|,,(,),{,},[,]

2,常用规则

修饰符

  • i 执行对大小写不敏感的匹配。
  • g 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。
  • m 执行多行匹配。

量词元字符

  • 前面的字符出现 0 或者 1 次 即可
  • + 前面的字符出现 1 次或 多次 即可
  • * 前面的字符出现 0 或 多次 即可
  • {n} 前面的字符连续出现 n 次 即可
  • {n,m} 前面的字符连续出现 n 到 m 次 即可
  • {n,} 前面的字符连续出现 n 到 多次 即可 |

特殊元字符

  • \b 单词边界
var reg = /\Bis\b/g;
var text = 'He is a boy, This is a dog. Where is she?';
var result = text.replace(reg,'IS');
// 'He is a boy, ThIS is a dog. Where is she?'
  • \B 非单词边界
  • \s 空白符
  • \S 非空白符
  • \d 0~9之间的数字
  • \D 除了0~9 之外的任意字符
  • \w 数字、字母、下划线(小写w)
  • \W 除了 数字、字母、下划线 的任意字符(大写W)
  • ^ 以什么字符开头
  • $ 以什么字符结尾
  • . 代表除了换行以外的所有字符
  • * 匹配零个或多个前面的那一个元素
  • .* 表示可匹配零个或多个任意字符
  • xa|yb 代表 或;xa或yb
  • [xyz] 代表 或;x或y或z //前后只能写单个字符
  • [^xy] 除了xy的任意字符

3,search、test、replace 函数

search()

返回子串的起始位置。

var str = "Visit Runoob!"; 
var n = str.search(/Runoob/i);  //6

test()

检测一个字符串是否匹配某个模式

var patt = /e/;  
patt.test("The best things in life are free!");  //true

replace()

替换一个与正则表达式匹配的子串

var str = document.getElementById("demo").innerHTML; 
var txt = str.replace(/microsoft/i,"Runoob");  //Visit Runoob!

exec()

检索字符串中的正则表达式的匹配

var patt = /li.e/;  
patt.exec("The best things in life are free!");  

// ['life', index: 19, input: 'The best things in life are free!', groups: undefined]

match()

将第一个匹配提取到变量

const match = "Hello World!".match(/hello/i);  
// ['Hello', index: 0, input: 'Hello World!', groups: undefined]

使用g标志,提取数组中的所有匹配项

const testString = "Repeat repeat rePeAT";
const regexWithAllMatches = /Repeat/gi;
testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]

match 与exec 分组区别

  • 无g
var s = "aaa1 bbb2 ccc3"; 
var reg = /\b(\w+)(\d{1})\b/;//两个分组,无g 
var rs_match1 = s.match(reg);  
var rs_match2 = s.match(reg); 
var rs_exec1 = reg.exec(s); 
var rs_exec2 = reg.exec(s);

console.log("match1:",rs_match1);
console.log("match2:",rs_match1);
console.log("exec1:",rs_exec1);
console.log("exec2:",rs_exec2);

/*
match1: (3) ['aaa1', 'aaa', '1', index: 0, input: 'aaa1 bbb2 ccc3', groups: undefined]
match2: (3) ['aaa1', 'aaa', '1', index: 0, input: 'aaa1 bbb2 ccc3', groups: undefined]
exec1: (3) ['aaa1', 'aaa', '1', index: 0, input: 'aaa1 bbb2 ccc3', groups: undefined]
exec2: (3) ['aaa1', 'aaa', '1', index: 0, input: 'aaa1 bbb2 ccc3', groups: undefined]
*/
  • 有g
var s = "aaa1 bbb2 ccc3";
var reg = /\b(\w+)(\d{1})\b/g;
var rs_match1 = s.match(reg);
var rs_match2 = s.match(reg);
var rs_exec1 = reg.exec(s);
var rs_exec2 = reg.exec(s);
var rs_exec3 = reg.exec(s);
var rs_exec4 = reg.exec(s);

console.log("match1:",rs_match1);
console.log("match2:",rs_match1);
console.log("exec1:",rs_exec1);
console.log("exec2:",rs_exec2);
console.log("exec3:",rs_exec3);
console.log("exec4:",rs_exec4);

/*
match1: (3) ['aaa1', 'bbb2', 'ccc3']
match2: (3) ['aaa1', 'bbb2', 'ccc3']
exec1: (3) ['aaa1', 'aaa', '1', index: 0, input: 'aaa1 bbb2 ccc3', groups: undefined]
exec2: (3) ['bbb2', 'bbb', '2', index: 5, input: 'aaa1 bbb2 ccc3', groups: undefined]
exec3: (3) ['ccc3', 'ccc', '3', index: 10, input: 'aaa1 bbb2 ccc3', groups: undefined]
exec4: null
*/

推荐:

欢迎关注我的前端自检清单,我和你一起成长