JavaScript正则表达式

正则基础

1. 匹配模式

  i: case-insensitive,表示忽略大小写
  g: global,表示全局匹配
  m: multiline,表示多行匹配 

2.元字符

   单词边界

var str = "what's your name";
var s = str.match(/[w']+/g); // ["what's", "your", "name"];

  B 不是单词边界

var str = "what's your name";
var s = str.match(/B[w']+B/g); // ["at", "ou", "am"];
// 说明: 单词边界包括空格或者开始第一个字母或者最好一个字母
var str = "what's your name";
var s = str.match(/Bw/); // ["h"];

  d 所有数字 [0-9]

var str = "13397518026";
var s = str.match(/d/); // "1"
var str = "13397518026";
var s = str.match(/^1[3-9][0-9]{9}$/); // "13397518026"

  D 非数字 [^0-9]

var str = "123abc*_()^&$%#@!~";
var s = str.match(/D/g); // ["a", "b", "c", "*", "_", "(", ")", "^", "&", "$", "%", "#", "@", "!", "~"]

  s 所有空白字符 [f v]

f -> 换页符

 -> 换行符

 -> 回车符
	 -> 制表符
v -> 垂直制表符
var str = "a
b
c	dveffg";
var s = str.match(/s/g); // ["↵", "", "    ", "", ""];
console.log(str, s);

  S 非空白字符

var str = "a
b
c	dveffg";
var s = str.match(/S/g); // ["a", "b", "c", "d", "e", "f", "g"]
console.log(str, s);

  w 字符 数字 下划线

var str = "abc123__你好";
var s = str.match(/w/g); // ["a", "b", "c", "1", "2", "3", "_", "_"]
console.log(str, s);

  W 不是数字字符下划线

var str = "abc123__你好!@#$";
var s = str.match(/W/g); // ["你", "好", "!", "@", "#", "$"]
console.log(str, s);

3.特殊字符

  . 除换行以外任意字符

var str = "aA13_你好!@#$-
-	-f-
";
var s = str.match(/./g); ["a", "A", "1", "3", "_", "你", "好", "!", "@", "#", "$", "-", "-", "    ", "-", "", "-"] // 不能匹配 
和 

console.log(str, s);

  * 0次或多次

var str = "1334abcdef";
var s = str.match(/d*B/); // "1334" 注: B是为了防止注释冲突
console.log(str, s);

 + 1次或多次

var str = "12345d67890";
var s = str.match(/d+/); // "12345"
console.log(s);
// 匹配其中的数字
var str = "abc1234567890abc";
var s = str.match(/d+/); // "1234567890"
console.log(s);

  ? 0次或1次

var str = "1234567890"; 
var s = str.match(/d?/); // "1"
console.log(s);
// 匹配0次将会是开始位置
var str = "aa1234567890";
var s = str.match(/d?/); // ""
console.log(s);

  ^ 以什么开头的字符串

var str = "aa1234567890";
var s = str.match(/^w+/); // "aa1234567890" 匹配以数字字母下划开头
console.log(s);

  $ 以什么结尾的字符串

var str = "ABC123456";
var s = str.match(/d+$/); // "123456"
console.log(s);

   转义符号

var str = "AB\d123456";
var s = str.match(/\d/); // "d"
console.log(str,s);

  | 条件分支或者

var str = "AB\d123456";
var s = str.match(/1|2|6/g); // ["1", "2", "6"]
console.log(str,s);

注: 或者前一个分支的分组将不会在下一个分组记录,也就是上一个分支的分组下一个分支不能使用

4.匹配次数
  {n} n次

var str = "AB123456";
var s = str.match(/d{3}/); // "123"     匹配三次数字  
console.log(str,s);

  {n,} n次及以上

var str = "AB123456";
var s = str.match(/d{3,}/); // "123456"     匹配三次以上的数字字符  
console.log(str,s);

  {n,m} n至m次

var str = "AB123456";
var s = str.match(/d{3,5}/); // "12345"     匹配三次或五次,尽可能多的匹配  
console.log(str,s);

  () 分组 后向引用 数字 代表分组

var str = "abc1234567890abc";
var s = str.match(/^(abc)d+1$/); 
console.log(s);

  (?'name'exp) | (?<name>exp) 分组的命名 后向引用 ame

var str = 'abc1331010456abc';
var s = str.match(/^(?<code>abc)d+1/); // 分组命名将会存在匹配的对象中 groups: {code: "abc"}
console.log(s);

  (?:exp) 不捕获匹配到的文本也不给分组

var str = 'abc1331010456abc';
var s = str.match(/^(?:abc)d+1/); // 取消分组之后将会匹配失败因为1 并没有任何分组
console.log(s);

5.零宽断言

 (?=exp) exp前面的位置

var str = 'abc1331010456abc';
var s = str.match(/d+(?=abc)/); // "1331010456" 表示匹配abc前面的所有数字,最少一个数字
console.log(s);

  (?<=exp) exp后面的位置

var str = 'def1331010456abc';
var s = str.match(/(?<=def)d+/); // "1331010456" 表示匹配def后面的所有数字,最少一个数字
console.log(s);

  (?!exp) 后面跟的不是exp的位置

var str = 'abcd_abce_abcg';
var s = str.match(/abc(?!d|e)/); // 表示匹配abc字符串,并且abc后面跟的字母不能是d或者e;
console.log(s);

  (?<!exp) 前面不是exp的位置

var str = 'abc123_000123_opq123';
var s = str.match(/(?<![a-z])123/); // 表示匹配字符串123,并且筛选出123前面为非字母的;
console.log(s);

6.惰性匹配 

 *?, +?, ??, {n,m}?, {n,}? 懒惰尽可能少的取匹配

var str = "123456";
var s = str.match(/d*?/); // ""    尽可能少的匹配数字字符
var s1 = str.match(/d+?/); // "1"    尽可能少的匹配数字字符
var s2 = str.match(/d??/); // ""    尽可能少的匹配数字字符
var s3 = str.match(/d{3,5}?/); // "123"    尽可能少的匹配数字字符
var s4 = str.match(/d{2,}?/); // "12"    尽可能少的匹配数字字符
console.log(str,s,s1,s2,s3,s4);

  *? 懒惰匹配

var str = "1334abcdef";
var s = str.match(/d*?/); // "" 尽可能少的匹配
console.log(str, s);
var str = "abcdef";
var s = str.match(/d*/); // ""
var r = str.replace(/d*/, '0') // 0abcdef
console.log(str, s);
console.log(r);
// 注意: 任意字符后面跟一个 * 号匹配,如果能够匹配到有效字符则会匹配有效字符,如果匹配不到有效字符将会匹配第一个开始位置

 [^x] 除某些字符以外

var str = "AB123456";
var s = str.match(/[^d]/g); // ["A", "B"]    匹配不是数字的字符
console.log(str,s);
原文地址:https://www.cnblogs.com/litings/p/12692314.html