1、匹配正则

.test();

  1. const string = "Hello World!";
  2. const regex = /string/i;
  3. const allString = regex.test(string);

2、提取正则

.match();

1):(提取获取到的第一个变量)

  1. const string = "Hello World! Hello, everyone!";
  2. const regex = /hello/i;
  3. const allString = string.match(regex ); // hello

2):(提取匹配到的所有变量)

  1. const string = "Hello World! Hello, everyone!";
  2. const regex = /hello/gi;
  3. const allString = string.match(regex ); // ["hello", "hello"]

3、通配符.

. (可用作任何字符的占位)

  1. const string = "Hello World! Hello, everyone!";
  2. const regex = /.orld/gi;
  3. const allString = string.match(regex ); // ["World"]

4、忽略大小写(i)

5、匹配所有(g)

6、匹配单个可能的字符 []

  1. const string = "cat is eating, it is fat!";
  2. const regex = /[cef]at/gi;
  3. const allString = string.match(regex ); // ["cat ", "fat"]

7、匹配所有字母[a-z] / [A-Z] / [a-zA-Z]

8、匹配数字字母[a-z0-9]

9、否定字符集^

匹配不想拥有的字符:
eg: const regex = /[^aoeiu]/gi;

10、匹配一行中出现一次或多次的字符 +

  1. const aa = "chinese";
  2. const regex = /e+/gi;
  3. const result = aa.match(regex); // [ "e" ,"e" ]

11、匹配连续出现多次或零次的字符 *

  1. const string = "heyyyyy";
  2. const string1 = "bye";
  3. const regex = /hey*/gi;
  4. const result = string.match(regex); ["yyyyy"]
  5. const result = string1 .match(regex); null

12、惰性匹配

  • 字符串中与给定要求匹配的最小部分
  • 默认情况下,正则表达式是贪婪的(匹配满足给定要求的字符串的最长部分)
  • 使用 ? 阻止贪婪模式(惰性匹配 )

    1. const string = "chinese";
    2. const regex = /c[a-z]*e/gi;
    3. const lazyRegex = /c[a-z]*?e/gi;
    4. string.match(regex); // ["chinese"]
    5. string.match(lazyRegex); // ["chine"]

13、匹配字符串开头

要测试字符串开头的字符匹配,请使用插入符号^,但要放到开头,不要放到字符集中(会解析成否定字符集)

  1. const emmaAtFrontOfString = "Emma likes cats a lot.";
  2. const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
  3. const startingStringRegex = /^Emma/;
  4. startingStringRegex.test(emmaAtFrontOfString); // true
  5. startingStringRegex.test(emmaNotAtFrontOfString); // false

14、匹配结束字符串

测试字符串结尾是否以规定字符结尾,用$符匹配

  1. const emmaAtBackOfString = "The cats do not like Emma";
  2. const emmaNotAtBackOfString = "Emma loves the cats";
  3. const startingStringRegex = /Emma$/;
  4. startingStringRegex.test(emmaAtBackOfString); // true
  5. startingStringRegex.test(emmaNotAtBackOfString); // false

15、匹配所有字母和数字(\w)

\w为\word 简写

  1. const longHand = /[A-Za-z0-9_]+/;
  2. const shortHand = /\w+/;
  3. const numbers = "42";
  4. const myFavoriteColor = "magenta";
  5. longHand.test(numbers); // true
  6. shortHand.test(numbers); // true
  7. longHand.test(myFavoriteColor); // true
  8. shortHand.test(myFavoriteColor); // true

16、匹配除字母和数字外的所有字符(\W)

\W 表示 \w 的反义

  1. const noAlphaNumericCharRegex = /\W/gi;
  2. const weirdCharacters = "!_$!!";
  3. const alphaNumericCharacters = "ab283AD";
  4. noAlphaNumericCharRegex.test(weirdCharacters); // true
  5. noAlphaNumericCharRegex.test(alphaNumericCharacters); // false

17、匹配所有数字(\d)

可以使用字符集[0-9],或者使用简写 \d

  1. const digitsRegex = /\d/g;
  2. const stringWithDigits = "My cat eats $20.00 worth of food a week.";
  3. stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]

18、匹配所有非数字

\D 表示 \d 的反义

  1. const nonDigitsRegex = /\D/g;
  2. const stringWithLetters = "101 degrees";
  3. stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]

19、匹配空格(\s)/非空格(\S)

20、匹配字符数

使用 {下界,上界} 指定一行中的特定字符数

  1. const regularHi = "hi";
  2. const mediocreHi = "hiii";
  3. const superExcitedHey = "heeeeyyyyy!!!";
  4. const excitedRegex = /hi{1,4}/;
  5. excitedRegex.test(regularHi); // true
  6. excitedRegex.test(mediocreHi); // true
  7. excitedRegex.test(superExcitedHey); //false

21、匹配最低字符数

使用{下界, }定义最少数量的字符要求,下面示例表示字母 i 至少要出现2次

  1. const regularHi = "hi";
  2. const mediocreHi = "hiii";
  3. const superExcitedHey = "heeeeyyyyy!!!";
  4. const excitedRegex = /hi{2,}/;
  5. excitedRegex.test(regularHi); // false
  6. excitedRegex.test(mediocreHi); // true
  7. excitedRegex.test(superExcitedHey); //false

22、匹配精确的字符数

使用{requiredCount}指定字符要求的确切数量

  1. const regularHi = "hi";
  2. const bestHi = "hii";
  3. const mediocreHi = "hiii";
  4. const excitedRegex = /hi{2}/;
  5. excitedRegex.test(regularHi); // false
  6. excitedRegex.test(bestHi); // true
  7. excitedRegex.test(mediocreHi); //false

23、匹配0次或1次

使用 ? 匹配字符 0 次或1次

  1. const britishSpelling = "colour";
  2. const americanSpelling = "Color";
  3. const languageRegex = /colou?r/i;
  4. languageRegex.test(britishSpelling); // true
  5. languageRegex.test(americanSpelling); // true