2.3 KiB
2.3 KiB
id, challengeType, forumTopicId, localeTitle
id | challengeType | forumTopicId | localeTitle |
---|---|---|---|
5c3dda8b4d8df89bea71600f | 1 | 301339 | 检查混合字符组 |
Description
()
来检查字符组。
如果想在字符串找到 Penguin
或 Pumpkin
,可以这个正则表达式:/P(engu|umpk)in/g
。
然后使用 test()
方法检查 test 字符串里面是否包含字符组。
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/g;
testRegex.test(testStr);
// Returns true
Instructions
Franklin Roosevelt
或 Eleanor Roosevelt
的名字,并且应该忽略 middle names。
然后完善代码,使创建的正则检查 myString
,根据正则是否匹配返回 true
或 false
。
Tests
tests:
- text: 正则 <code>myRegex</code> 测试 <code>Franklin D. Roosevelt</code> 应该返回 <code>true</code>。
testString: myRegex.lastIndex = 0; assert(myRegex.test('Franklin D. Roosevelt'));
- text: 正则 <code>myRegex</code> 测试 <code>Eleanor Roosevelt</code> 应该返回 <code>true</code>。
testString: myRegex.lastIndex = 0; assert(myRegex.test('Eleanor Roosevelt'));
- text: 正则 <code>myRegex</code> 测试 <code>Franklin Rosevelt</code> 应该返回 <code>false</code>。
testString: myRegex.lastIndex = 0; assert(!myRegex.test('Franklin Rosevelt'));
- text: 应该使用 <code>.test()</code> 来测试正则。
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
- text: result 应该返回 <code>true</code>。
testString: assert(result === true);
Challenge Seed
let myString = "Eleanor Roosevelt";
let myRegex = /False/; // Change this line
let result = false; // Change this line
// After passing the challenge experiment with myString and see how the grouping works
Solution
let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
let result = myRegex.test(myString);