JavaScript 正则表达式
使用测试方法
在编程语言中,正则表达式用于匹配指定的字符串。 通过正则表达式创建匹配模式(规则)可以帮你完成指定匹配。
如果想要在字符串 The dog chased the cat 中匹配到 the 这个单词,可以使用如下正则表达式:/the/。 注意,正则表达式中不需要引号。
JavaScript 中有多种使用正则表达式的方法。 测试正则表达式的一种方法是使用 .test() 方法。 .test() 方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回 true,反之,返回 false。
let testStr = “freeCodeCamp”;
let testRegex = /Code/;
testRegex.test(testStr);
test 方法会返回 true。
使用 .test() 方法,检测字符串 myString 是否符合正则表达式 myRegex 定义的规则。
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // 修改这一行
匹配文字字符串
在上一个挑战中,使用正则表达式 /Hello/ 搜索到了字符串 Hello。 那个正则表达式在字符串中搜寻 Hello 的文字匹配。 下面是另一个在字符串中搜寻 Kevin 的示例:
let testStr = “Hello, my name is Kevin.”;
let testRegex = /Kevin/;
testRegex.test(testStr);
test 方法会返回 true。
任何其他形式的 Kevin 都不会被匹配。 例如,正则表达式 /Kevin/ 不会匹配 kevin 或者KEVIN。
let wrongRegex = /kevin/;
wrongRegex.test(testStr);
此 test 调用将返回 false。
后续的挑战将为你展示如何匹配其他形式的字符串。
完成正则表达式 waldoRegex,在字符串 waldoIsHiding 中匹配到文本 “Waldo”。
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
let waldoRegex = /Waldo/; // 修改这一行
let result = waldoRegex.test(waldoIsHiding);
同时用多种模式匹配文字字符串
使用正则表达式/coding/,你可以在其他字符串中查找coding。
这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。 你可以使用 alternation 或 OR 操作符搜索多个模式: |
此操作符匹配操作符前面或后面的字符。 例如,如果你想匹配 yes 或 no,你需要的正则表达式是 /yes|no/。
你还可以匹配多个规则,这可以通过添加更多的匹配模式来实现。 这些匹配模式将包含更多的 OR 操作符来分隔它们,比如/yes|no|maybe/。
完成正则表达式 petRegex 以匹配 dog、cat、bird 或者 fish。
let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/; // 修改这一行
let result = petRegex.test(petString);
匹配时忽略大小写
到目前为止,已经了解了如何用正则表达式来执行字符串的匹配。 但有时候,并不关注匹配字母的大小写。
大小写即大写字母和小写字母。 大写字母如 A、B 和 C。 小写字母如 a、b 和 c。
可以使用标志(flag)来匹配这两种情况。 标志有很多,不过这里我们只关注忽略大小写的标志——i。 可以通过将它附加到正则表达式之后来使用它。 这里给出使用该标志的一个实例 /ignorecase/i。 这个字符串可以匹配字符串 ignorecase、igNoreCase 和 IgnoreCase。
编写正则表达式 fccRegex 以匹配 freeCodeCamp,忽略大小写。 正则表达式不应与任何缩写或带有空格的变体匹配。
let myString = "freeCodeCamp";
let fccRegex = /freecodecamp/i; // 修改这一行
let result = fccRegex.test(myString);
提取匹配项
到目前为止,只是检查了一个匹配模式是否存在于字符串中。 还可以使用 .match() 方法来提取找到的实际匹配项。
可以使用字符串来调用 .match() 方法,并在括号内传入正则表达式。
请看下面的举例:
“Hello, World!”.match(/Hello/);
let ourStr = “Regular expressions”;
let ourRegex = /expressions/;
ourStr.match(ourRegex);
这里第一个 match 将返回 [“Hello”] 第二个将返回 [“expressions”]。
请注意, .match 语法是目前为止一直使用的 .test 方法中的“反向”:
‘string’.match(/regex/);
/regex/.test(‘string’)
利用 .match() 方法提取单词 coding。
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // 修改这一行
let result = extractStr.match(codingRegex); // 修改这一行
全局匹配
到目前为止,只能提取或搜寻一次模式匹配。
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 单词并提取出来。
注意:
在正则表达式上可以有多个标志,比如 /search/gi
let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /Twinkle/gi; // 修改这一行
let result = twinkleStar.match(starRegex); // 修改这一行
用通配符匹配任何内容
有时不(或不需要)知道匹配模式中的确切字符。 如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。 幸运的是,可以使用通配符 . 来处理这种情况。
通配符 . 将匹配任何一个字符。 通配符也叫 dot 或 period。 可以像使用正则表达式中任何其他字符一样使用通配符。 例如,如果想匹配 hug、huh、hut 和 hum,可以使用正则表达式 /hu./ 匹配以上四个单词。
let humStr = “I’ll hum a song”;
let hugStr = “Bear hug”;
let huRegex = /hu./;
huRegex.test(humStr);
huRegex.test(hugStr);
上面的 test 都会返回 true。
完成正则表达式 unRegex 以匹配字符串 run、sun、fun、pun、nun 和 bun。 正则表达式中应该使用通配符。
let exampleStr = "Let's have fun with regular expressions!"