正则关卡14:用惰性匹配来查找字符

正则表达式测试工具

在正则表达式中,贪婪匹配会匹配到符合正则表达式匹配模式的字符串的最长可能部分,并将其作为匹配项返回。
另一种方案称为懒惰匹配,它会匹配到满足正则表达式的字符串的最小可能部分。
可以将正则表达式/t[a-z]*i/应用于字符串"titanic"
这个正则表达式是一个以t开始,以i结束,并且中间有一些字母的匹配模式。
正则表达式默认是贪婪匹配,因此匹配返回为["titani"]
它会匹配到适合该匹配模式的最大子字符串。
但是,你可以使用?字符来将其变成懒惰匹配。
调整后的正则表达式/t[a-z]*?i/匹配字符串"titanic"返回["ti"]

var str = "titanic";
var regex1 = /t[a-z]*i/;
var regex2 = /t[a-z]*?i/;
console.log( str.match(regex1) ); // 返回为 ["titani"]
console.log( str.match(regex2) ); // 返回 ["t

注意应该避免使用正则表达式解析 HTML,但是可以用正则表达式匹配 HTML 字符串。

闯关:修复正则表达式/<.*>/,让它返回 HTML 标签<h1>,而不是文本"<h1>Winter is coming</h1>"
请记得在正则表达式中使用通配符.来匹配任意字符。

var text = "<h1>Winter is coming</h1>";
var myRegex = /<.*>/; // 修改这行
var result = text.match(myRegex);
console.log(result);

正确代码

var text = "<h1>Winter is coming</h1>";
var myRegex = /<.*?>/;
var result = text.match(myRegex);
console.log(result);


正则关卡13:匹配出现零次或多次的字符

正则表达式测试工具

+用来查找出现一次或多次的字符。
​可以匹配出现零次或多次的字符。
例如:

var soccerWord = "gooooooooal!";
var gPhrase = "gut feeling";
var oPhrase = "over the moon";
var goRegex = /go*/;
console.log( soccerWord.match(goRegex) ); // 返回 ["goooooooo"]
console.log( gPhrase.match(goRegex) ); // 返回 ["g"]
console.log( oPhrase.match(goRegex) ); // 返回 null

闯关:创建一个变量为chewieRegex的正则表达式,使用*符号在chewieQuote中匹配"A"及其之后出现的零个或多个"a"

注意: 你的正则表达式不需要使用修饰符,也不需要匹配引号。

var chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
var chewieRegex = /change/; // 修改这行代码
var result = chewieQuote.match(chewieRegex);
console.log(result);

正确代码

var chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
var chewieRegex = /Aa*/g;
var result = chewieQuote.match(chewieRegex);
console.log(result);

正则关卡12:匹配出现一次或多次的字符

正则表达式测试工具

有时,需要匹配出现一次或者连续多次的的字符(或字符组)。
这意味着它至少出现一次,并且可能重复出现。
可以使用+符号来检查情况是否如此。
记住,字符或匹配模式必须一个接一个地连续出现。
例如,/a+/g会在"abc"中匹配到一个匹配项,并且返回["a"]
因为+的存在,它也会在"aabc"中匹配到一个匹配项,然后返回["aa"]
如果它是检查字符串"abab",它将匹配到两个匹配项并且返回["a", "a"],因为a字符不连续,在它们之间有一个b字符。
最后,因为在字符串"bcd"中没有"a",因此找不到匹配项。

var str1 = "abc";
var str2 = "aabc";
var str3 = "abab";
var str4 = "bcd";
var regex = /a+/g;
console.log( str1.match(regex) ); // 返回 ["a"]
console.log( str2.match(regex) ); // 返回 ["aa"]
console.log( str3.match(regex) ); // 返回 ["a", "a"]
console.log( str4.match(regex) ); // 返回 null

闯关:想要在字符串"Mississippi"中匹配到出现一次或多次的字母s的匹配项。
编写一个使用+符号的正则表达式。

var difficultSpelling = "Mississippi";
var myRegex = /change/; // 修改这行
var result = difficultSpelling.match(myRegex);
console.log(result);

正确代码

var difficultSpelling = "Mississippi";
var myRegex = /s+/ig;
var result = difficultSpelling.match(myRegex);
console.log(result);

正则关卡11:创建否定字符集

正则表达式测试工具

到目前为止,已经创建了一个想要匹配的字符集合,

正则表达式也支持创建一个不想匹配的字符集合。

要创建否定字符集,需要在开始括号后面和不想匹配的字符前面放置插入字符(即^)。
例如,/[^aeiou]/gi匹配所有非元音字符。

var quoteSample = "When you were born,you were crying and everyone around you was smiling.";
var vowelRegex = /[^aeiou]/gi; // 修改这行
var result = quoteSample.match(vowelRegex); // 修改这行
console.log(result);

注意,字符.![@/和空白字符等也会被匹配,该否定字符集仅排除元音字符。

闯关:创建一个匹配所有非数字或元音字符的正则表达式。
请记得在正则表达式中包含恰当的标志。

var quoteSample = "3 blind mice.";
var myRegex = /change/; // 修改这行
var result = myRegex; // 修改这行
console.log(result);

正确代码

var quoteSample = "3 blind mice.";
var myRegex = /([^0-9aeiou])/ig;
var result = quoteSample.match(myRegex);
console.log(result);

正则关卡10:匹配字母表中的数字和字母

正则表达式测试工具

使用连字符(-)匹配字符范围并不仅限于字母。
它还可以匹配一系列数字。
例如,/[0-5]/匹配05之间的任意数字,包含05
此外,还可以在单个字符集中组合一系列字母和数字。

let kellyStr = "Kelly18250871007";
let myRegex = /[a-z0-9]/ig;
// 匹配 kellyStr 中所有的字母和数字
var kellyMatch = kellyStr.match(myRegex);
console.log(kellyMatch);

闯关:创建一个正则表达式,使其可以匹配hs之间的一系列字母,以及26之间的一系列数字。

提示:请记得在正则表达式中包含恰当的标志。

var quoteSample = "Blueberry 3.141592653s are delicious.";
var myRegex = /change/; // 修改这行
var result = myRegex; // 修改这行
console.log(result);

正确代码

var quoteSample = "Blueberry 3.141592653s are delicious.";
var myRegex = /([h-s]|[2-6])/ig;
var result = quoteSample.match(myRegex);
console.log(result);

正则关卡9:匹配字母表中的字母

正则表达式测试工具

了解了如何使用字符集来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。
字符集中,可以使用连字符-)来定义要匹配的字符范围。
例如,要匹配小写字母ae,你可以使用[a-e]

var catStr = "cat";
var batStr = "bat";
var matStr = "mat";
var bgRegex = /[a-e]at/;
console.log( catStr.match(bgRegex) ); // 返回 ["cat"]
console.log( batStr.match(bgRegex) ); // 返回 ["bat"]
console.log( matStr.match(bgRegex) ); // 返回 null

闯关:匹配字符串quoteSample中的所有字母。
注意:一定要同时匹配大小写字母。

var quoteSample = "The quick brown fox jumps over the lazy dog.";
var alphabetRegex = /change/; // 修改这行
var result = alphabetRegex; // 修改这行
console.log(result);

正确代码

var quoteSample = "The quick brown fox jumps over the lazy dog.";
var alphabetRegex = /[a-z]/ig;
var result = quoteSample.match(alphabetRegex);
console.log(result);