发布于 3年前

JavaScript正则表达式使用小手册

以下是JavaScript使用正则表达式的一个备忘录。

测试正则表达式

test()方法:用来测试字符串是否满足表达式。

let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);

测试多个pattern

可以使用或操作符(|)来连接多个表达式

const regex = /yes|no|maybe/;

忽略大小写

使用“i”来忽略字符串的大小写

const caseInsensitiveRegex = /ignore case/i;
const testString = 'We use the i flag to iGnOrE CasE';
caseInsensitiveRegex.test(testString); // true

提取第一个匹配项

match()方法:它可以用来提取匹配项

const match = "Hello World!".match(/hello/i); // "Hello"

提取所有匹配项到数组

使用“g”标识可以提取所有的匹配项

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

匹配任何字符

通配符点“.”,它标识匹配任何字符。

// 匹配"cat", "BAT", "fAT", "mat"
const regexWithWildcard = /.at/gi;
const testString = "cat BAT cupcake fAT mat dog";
const allMatchingWords = testString.match(regexWithWildcard); // ["cat", "BAT", "fAT", "mat"]

匹配单个字符列表

把想要匹配的字符放到中括号中[],然后进行匹配。

// 匹配"cat" "fat" and "mat" but not "bat"
const regexWithCharClass = /[cfm]at/g;
const testString = "cat fat bat mat";
const allMatchingWords = testString.match(regexWithCharClass); // ["cat", "fat", "mat"]

匹配英文字母

使用表示范围的字符集“[a-z]”,其中杆(-)是连接符,表示从小写字母a到小写字母z。

const regexWithCharRange = /[a-e]at/;
const catString = "cat";
const batString = "bat";
const fatString = "fat";
regexWithCharRange.test(catString); // true
regexWithCharRange.test(batString); // true
regexWithCharRange.test(fatString); // false

匹配英文字母和数字

使用表示范围的字符集"[a-z0-9]"

const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
const testString = "Emma19382";
testString.match(regexWithLetterAndNumberRange) // true

匹配中文

使用访问字符集:“[\u4e00-\u9fa5]”

const regexWithChineseRange = /[\u4e00-\u9fa5]/;
const testString = "Emma19382中文";
testString.match(regexWithChineseRange) // true

匹配一个未知字符

要匹配不需要的字符集,可以使用取反(^)字符集

const allCharsNotVowels = /[^aeiou]/gi;
const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;

匹配连续出现一次或多次的字符

使用加号“+”表示字符连续出现一次或多次

const oneOrMoreAsRegex = /a+/gi;
const oneOrMoreSsRegex = /s+/gi;
const cityInFlorida = "Tallahassee";
cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];

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

使用星号“*”表示字符连续出现零次或多次

const zeroOrMoreOsRegex = /hi*/gi;
const normalHi = "hi";
const happyHi = "hiiiiii";
const twoHis = "hiihii";
const bye = "bye";
normalHi.match(zeroOrMoreOsRegex); // ["hi"]
happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
bye.match(zeroOrMoreOsRegex); // null

匹配字符串开头

使用“^”可以检测字符串开头的字符是否匹配。

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

匹配字符串结尾

使用“$”可以检测字符串的结尾的字符是否匹配。

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

匹配前面的子表达式零次或一次

匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。

const testString = "catastrophe";
const greedyRexex = /c[a-z]*t/gi;
const lazyRegex = /c[a-z]*?t/gi;
testString.match(greedyRexex); // ["catast"]
testString.match(lazyRegex); // ["cat"]

匹配所有字母和数字

转义\w:表示所有字母和数字。注意此处的“w”是小写的

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

匹配除字母和数字以外的所有内容

转义\W:与“\w”相反,它是匹配除字母和数字以外的所有内容。

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

匹配数字

转义\d:除了使用范围字符集“[0-9]”来匹配数字外,也可以使用此转义来匹配数字。

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

匹配非数字字符

转义\D:这和“\d”相反,它用来匹配非数字字符。

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

匹配空白字符

转义\s:表示匹配所有空白字符,其中包括空格,tab(\t),回车(\n)

const sentenceWithWhitespace = "I like cats!"
var spaceRegex = /\s/g;
whiteSpace.match(sentenceWithWhitespace); // [" ", " "]

匹配非空白字符

转义\S:与“\s”相反,它可以匹配所有非空白字符。

const sentenceWithWhitespace = "C a t"
const nonWhiteSpaceRegex = /\S/g;
sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]

匹配指定的字符数范围

{lowerBound,upperBound}表示匹配到的字符的最小数量和最大数量。

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

匹配的最少字符数

{lowerBound,}指定要匹配到最少数量的字符要求

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

匹配确切数目的字符数

{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

匹配全部字符或不匹配任何字符

使用?来检查字符是否存在

const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou?r/i;
languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true
©2020 edoou.com   京ICP备16001874号-3