全局匹配
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/;
testStr.match(ourRegex);
// 在这里 match 将返回 ["Repeat"]。
// 若要多次搜寻或提取模式匹配,可以使用 g 标志。
let repeatRegex = /Repeat/g;
testStr.match(repeatRegex);
// 这里 match 返回值 ["Repeat", "Repeat", "Repeat"]
使用正则表达式 starRegex,从字符串 twinkleStar 中匹配所有的 Twinkle 单词并提取出来。
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /Twinkle/gi; // 修改这一行
let result1 = twinkleStar.match(starRegex); // 修改这一行
console.log(result1);//["Twinkle", "twinkle"]
匹配字符串的开头(^)
在之前的挑战中,使用字符集中前插入符号(^)来创建一个否定字符集, (在[]里面的^为否定,其他情况为以...开始) 形如 [^thingsThatWillNotBeMatched]
。除了在字符集中使用之外,脱字符还用于匹配字符串的开始位置。
let firstString = "Ricky is first and can be found.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString);
let notFirst = "You can't find Ricky now.";
firstRegex.test(notFirst);
//第一次 test 调用将返回 true,而第二次调用将返回 false。
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // 是否以 Cal为开头
let calRegex1 = /^Cu/; // 是否以 Cu为开头
let result0 = calRegex.test(rickyAndCal);
let result111 = calRegex1.test(rickyAndCal);
console.log("^匹配字符串的开头==============");
console.log(result0); //true
console.log(result111);//false
匹配字符串的末尾
// 可以使用正则表达式的美元符号 $ 来搜寻字符串的结尾。
let theEnding = "This is a never ending story";
let storyRegex = /story$/;
storyRegex.test(theEnding);
let noEnding = "Sometimes a story will have to end";
storyRegex.test(noEnding);
// 第一次 test 调用将返回 true, 而第二次调用将返回 false。
let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/; // 修改这一行
let lastRegex1 = /coose$/; // 修改这一行
let result01 = lastRegex.test(caboose);
let result0011 = lastRegex1.test(caboose);
console.log(result01);//true
console.log(result0011);//false
匹配空白字符(s
)
可以使用 s
搜寻空格,其中 s 是小写。此匹配模式将匹配空格、回车符、制表符、换页符和换行符
let whiteSpace = "Whitespace. Whitespace everywhere!"
let spaceRegex = /s/g;
whiteSpace.match(spaceRegex);
// 这个 match 调用将返回 [" ", " "]。
指定匹配的上限和下限
使用加号 + 查找一个或多个字符,使用星号 * 查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。,要匹配出现 3 到 5 次字母 a 的在字符串 ah, 正则表达式应为/a{3,5}h/
。
let A4 = "aaaah";
let A2 = "aah";
let multipleA = /a{3,5}h/;
multipleA.test(A4);
multipleA.test(A2);
// 第一次 test 调用将返回 true,而第二次调用将返回 false。
只指定匹配的下限 可以使用带有花括号的数量说明符来指定匹配模式的上下限。但有时候只想指定匹配模式的下限而不需要指定上限。
为此,在第一个数字后面跟一个逗号即可。
例如,要匹配至少出现 3 次字母 a 的字符串 hah, 正则表达式应该是 /ha{3,}h/。
let A4 = "haaaah";
let A2 = "haah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleA = /ha{3,}h/;
multipleA.test(A4);
multipleA.test(A2);
multipleA.test(A100);
// 按顺序排列,三次 test 调用将返回值 true,false 和 true。
正向先行断言和负向先行断言(重点)
(?!pattern) 负向先行断言
代表字符串中的一个位置,紧接该位置之后的字符序列不能匹配 pattern。
例如对 "regex represents regular expression" 这个字符串,
要想匹配除 regex 和 regular 之外的 re,可以用 re(?!g),
该表达式限定了 re 右边的位置,这个位置后面不是字符 g。
(?=pattern) 正向先行断言
代表字符串中的一个位置,紧接该位置之后的字符序列能够匹配 pattern。
例如对 "a regular expression" 这个字符串,
要想匹配 regular 中的 re,但不能匹配 expression 中的 re,可以用 re(?=gular),
该表达式限定了 re 右边的位置,这个位置之后是 gular,但并不消耗 gular 这些字符。
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex);
noquit.match(qRegex);
// 这两次 match 调用都将返回 ["q"]。
原文始发于微信公众号(迪哥讲事):JS基本功系列-正则表达式
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论