1、匹配正则
.test();
const string = "Hello World!";
const regex = /string/i;
const allString = regex.test(string);
2、提取正则
1):(提取获取到的第一个变量)
const string = "Hello World! Hello, everyone!";
const regex = /hello/i;
const allString = string.match(regex ); // hello
2):(提取匹配到的所有变量)
const string = "Hello World! Hello, everyone!";
const regex = /hello/gi;
const allString = string.match(regex ); // ["hello", "hello"]
3、通配符.
. (可用作任何字符的占位)
const string = "Hello World! Hello, everyone!";
const regex = /.orld/gi;
const allString = string.match(regex ); // ["World"]
4、忽略大小写(i)
5、匹配所有(g)
6、匹配单个可能的字符 []
const string = "cat is eating, it is fat!";
const regex = /[cef]at/gi;
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、匹配一行中出现一次或多次的字符 +
const aa = "chinese";
const regex = /e+/gi;
const result = aa.match(regex); // [ "e" ,"e" ]
11、匹配连续出现多次或零次的字符 *
const string = "heyyyyy";
const string1 = "bye";
const regex = /hey*/gi;
const result = string.match(regex); ["yyyyy"]
const result = string1 .match(regex); null
12、惰性匹配
- 字符串中与给定要求匹配的最小部分
- 默认情况下,正则表达式是贪婪的(匹配满足给定要求的字符串的最长部分)
使用
?
阻止贪婪模式(惰性匹配 )const string = "chinese";
const regex = /c[a-z]*e/gi;
const lazyRegex = /c[a-z]*?e/gi;
string.match(regex); // ["chinese"]
string.match(lazyRegex); // ["chine"]
13、匹配字符串开头
要测试字符串开头的字符匹配,请使用插入符号^
,但要放到开头,不要放到字符集中(会解析成否定字符集)
const emmaAtFrontOfString = "Emma likes cats a lot.";
const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
const startingStringRegex = /^Emma/;
startingStringRegex.test(emmaAtFrontOfString); // true
startingStringRegex.test(emmaNotAtFrontOfString); // false
14、匹配结束字符串
测试字符串结尾是否以规定字符结尾,用$符匹配
const emmaAtBackOfString = "The cats do not like Emma";
const emmaNotAtBackOfString = "Emma loves the cats";
const startingStringRegex = /Emma$/;
startingStringRegex.test(emmaAtBackOfString); // true
startingStringRegex.test(emmaNotAtBackOfString); // false
15、匹配所有字母和数字(\w)
\w为\word 简写
const longHand = /[A-Za-z0-9_]+/;
const shortHand = /\w+/;
const numbers = "42";
const myFavoriteColor = "magenta";
longHand.test(numbers); // true
shortHand.test(numbers); // true
longHand.test(myFavoriteColor); // true
shortHand.test(myFavoriteColor); // true
16、匹配除字母和数字外的所有字符(\W)
用\W
表示 \w
的反义
const noAlphaNumericCharRegex = /\W/gi;
const weirdCharacters = "!_$!!";
const alphaNumericCharacters = "ab283AD";
noAlphaNumericCharRegex.test(weirdCharacters); // true
noAlphaNumericCharRegex.test(alphaNumericCharacters); // false
17、匹配所有数字(\d)
可以使用字符集[0-9]
,或者使用简写 \d
const digitsRegex = /\d/g;
const stringWithDigits = "My cat eats $20.00 worth of food a week.";
stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]
18、匹配所有非数字
用\D
表示 \d
的反义
const nonDigitsRegex = /\D/g;
const stringWithLetters = "101 degrees";
stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]
19、匹配空格(\s)/非空格(\S)
20、匹配字符数
使用 {下界,上界}
指定一行中的特定字符数
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = /hi{1,4}/;
excitedRegex.test(regularHi); // true
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false
21、匹配最低字符数
使用{下界, }
定义最少数量的字符要求,下面示例表示字母 i
至少要出现2次
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = /hi{2,}/;
excitedRegex.test(regularHi); // false
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false
22、匹配精确的字符数
使用{requiredCount}
指定字符要求的确切数量
const regularHi = "hi";
const bestHi = "hii";
const mediocreHi = "hiii";
const excitedRegex = /hi{2}/;
excitedRegex.test(regularHi); // false
excitedRegex.test(bestHi); // true
excitedRegex.test(mediocreHi); //false
23、匹配0次或1次
使用 ?
匹配字符 0 次或1次
const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou?r/i;
languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true