2018-10-10 18:03:03 -04:00
---
id: 587d7dba367417b2b2512ba8
2021-02-06 04:42:36 +00:00
title: Check for All or None
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:14:01 +08:00
forumTopicId: 301338
2021-01-13 03:31:00 +01:00
dashedName: check-for-all-or-none
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
You can specify the possible existence of an element with a question mark, `?` . This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
2020-08-04 15:14:01 +08:00
```js
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your regex should use the optional symbol, `?` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
favRegex.lastIndex = 0;
2020-12-16 00:37:30 -07:00
assert(favRegex.source.match(/\?/).length > 0);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your regex should match `"favorite"`
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
favRegex.lastIndex = 0;
2020-12-16 00:37:30 -07:00
assert(favRegex.test('favorite'));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your regex should match `"favourite"`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
favRegex.lastIndex = 0;
2020-12-16 00:37:30 -07:00
assert(favRegex.test('favourite'));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your regex should not match `"fav"`
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
favRegex.lastIndex = 0;
2020-12-16 00:37:30 -07:00
assert(!favRegex.test('fav'));
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:14:01 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
let favWord = "favorite";
let favRegex = /change/; // Change this line
let result = favRegex.test(favWord);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
let favWord = "favorite";
let favRegex = /favou?r/;
let result = favRegex.test(favWord);
```