freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/find-characters-with-lazy-m...

1.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db6367417b2b2512b9b Find Characters with Lazy Matching 1 查找具有延迟匹配的字符

Description

在正则表达式中, greedy匹配找到符合正则表达式模式的字符串的最长部分,并将其作为匹配返回。替代方案称为lazy匹配它找到满足正则表达式模式的字符串的最小可能部分。您可以将regex /t[az]*i/应用于字符串"titanic" 。这个正则表达式基本上是一个以t开头的模式,以i结尾,并且在它们之间有一些字母。默认情况下,正则表达式是greedy ,因此匹配将返回["titani"] 。它找到可能适合模式的最大子串。但是,你可以使用?字符将其更改为lazy匹配。 "titanic"与调整后的正则表达式匹配/t[az]*?i/ return ["ti"]

Instructions

修复regex /<.*>/以返回HTML标记<h1>而不是文本"<h1>Winter is coming</h1>" 。记住通配符.在正则表达式中匹配任何字符。

Tests

tests:
  - text: <code>result</code>变量应该是一个包含<code>&lt;h1&gt;</code>的数组
    testString: 'assert(result[0] == "<h1>", "The <code>result</code> variable should be an array with <code>&lt;h1&gt;</code> in it");'

Challenge Seed

let text = "<h1>Winter is coming</h1>";
let myRegex = /<.*>/; // Change this line
let result = text.match(myRegex);

Solution

// solution required