match()
方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法类似 indexOf()
和 lastIndexOf()
,但是它返回指定的值,而不是字符串的位置。
可以使用字符串来调用match()
方法,并在括号内传入正则表达式。
例如:
var matchArr = "Hello, World!".match(/Hello/); // 返回 ["Hello"]
console.log(matchArr);
var ourStr = "Regular expressions";
var ourRegex = /expressions/;
var ourArr = ourStr.match(ourRegex); // 返回 ["expressions"]
console.log(ourArr);
控制台运行截图:

闯关:利用.match()
方法提取单词coding
。
var extractStr = "Extract the word 'coding' from this string.";
var codingRegex = /change/; // 修改这行
var result = extractStr; // 修改这行
console.log(result);
正确代码
var extractStr = "Extract the word 'coding' from this string.";
var codingRegex = /coding/;
var result = extractStr.match(codingRegex);
console.log(result);