2.1 KiB
2.1 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7db7367417b2b2512b9d | Match Beginning String Patterns | 1 | 301349 | 匹配字符串的开头 |
Description
字符集
中的插入
符号(^
)来创建一个否定字符集
,形如[^thingsThatWillNotBeMatched]
。在字符集
之外,插入
符号用于字符串的开头搜寻匹配模式。
let firstString = "Ricky is first and can be found.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString);
// Returns true
let notFirst = "You can't find Ricky now.";
firstRegex.test(notFirst);
// Returns false
Instructions
^
符号,以匹配仅在字符串rickyAndCal
的开头出现的"Cal"
。
Tests
tests:
- text: "你的正则表达式应该搜寻有一个大写字母的<code>'Cal'</code>。"
testString: assert(calRegex.source == "^Cal");
- text: 你的正则表达式不应该使用任何标志。
testString: assert(calRegex.flags == "");
- text: "你的正则表达式应该匹配字符串开头的<code>'Cal'</code>。"
testString: assert(calRegex.test("Cal and Ricky both like racing."));
- text: "你的正则表达式不应该匹配字符串中间的<code>'Cal'</code>。"
testString: assert(!calRegex.test("Ricky and Cal both like racing."));
Challenge Seed
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /change/; // Change this line
let result = calRegex.test(rickyAndCal);
Solution
let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal);