1.8 KiB
1.8 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db4367417b2b2512b92 | マッチしたものを抽出する | 1 | 301340 | extract-matches |
--description--
ここまでは、単にパターンが文字列内に存在するかどうかをチェックしてきました。 .match() メソッドで見つけた実際のマッチ部分を抽出することもできます。
.match() メソッドを使用するには、メソッドを文字列に適用し、括弧内に正規表現を渡します。
例を示します。
"Hello, World!".match(/Hello/);
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
ここで最初の match は ["Hello"] を返し、2 番目の match は ["expressions"] を返します。
.match の構文は、これまで使用してきた .test メソッドの「反対」であることに注意してください。
'string'.match(/regex/);
/regex/.test('string');
--instructions--
.match() メソッドを適用して、文字列 coding を抽出してください。
--hints--
result には文字列 coding が存在する必要があります。
assert(result.join() === 'coding');
正規表現 codingRegex で文字列 coding を検索する必要があります。
assert(codingRegex.source === 'coding');
.match() メソッドを使用する必要があります。
assert(code.match(/\.match\(.*\)/));
--seed--
--seed-contents--
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
--solutions--
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line