2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7dba367417b2b2512ba8 | Check for All or None | 1 | 301338 |
Description
?
. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
Instructions
favRegex
to match both the American English (favorite) and the British English (favourite) version of the word.
Tests
tests:
- text: Your regex should use the optional symbol, <code>?</code>.
testString: favRegex.lastIndex = 0; assert(favRegex.source.match(/\?/).length > 0);
- text: Your regex should match <code>"favorite"</code>
testString: favRegex.lastIndex = 0; assert(favRegex.test("favorite"));
- text: Your regex should match <code>"favourite"</code>
testString: favRegex.lastIndex = 0; assert(favRegex.test("favourite"));
- text: Your regex should not match <code>"fav"</code>
testString: favRegex.lastIndex = 0; assert(!favRegex.test("fav"));
Challenge Seed
let favWord = "favorite";
let favRegex = /change/; // Change this line
let result = favRegex.test(favWord);
Solution
let favWord = "favorite";
let favRegex = /favou?r/;
let result = favRegex.test(favWord);