diff --git a/curriculum/challenges/_meta/regular-expressions/meta.json b/curriculum/challenges/_meta/regular-expressions/meta.json index 633532f6e0..2effbe77f2 100644 --- a/curriculum/challenges/_meta/regular-expressions/meta.json +++ b/curriculum/challenges/_meta/regular-expressions/meta.json @@ -124,6 +124,10 @@ "587d7dba367417b2b2512ba9", "Positive and Negative Lookahead" ], + [ + "5c3dda8b4d8df89bea71600f", + "Check For Mixed Grouping of Characters" + ], [ "587d7dbb367417b2b2512baa", "Reuse Patterns Using Capture Groups" diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.english.md new file mode 100644 index 0000000000..11d49e2043 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.english.md @@ -0,0 +1,66 @@ +--- +id: 5c3dda8b4d8df89bea71600f +title: Check For Mixed Grouping of Characters +challengeType: 1 +--- + +## Description +
+Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses (). +If you want to find either Penguin or Pumpkin in a string, you can use the following Regular Expression: /P(engu|umpk)in/g +Then check whether the desired string groups are in the test string by using the test() method. +
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/g;
testRegex.test(testStr);
// Returns true
+
+ +## Instructions +
+Fix the regex so that it checks for the names of Franklin Roosevelt or Eleanor Roosevelt in a case sensitive manner and it should make concessions for middle names. +Then fix the code so that the regex that you have created is checked against myString and either true or false is returned depending on whether the regex matches. +
+ +## Tests +
+ +```yml +tests: + - text: Your regex myRegex should return true for the string Franklin D. Roosevelt + testString: assert(myRegex.test('Franklin D. Roosevelt'), 'Your regex myRegex should return true for the string Franklin D. Roosevelt'); + - text: Your regex myRegex should return true for the string Eleanor Roosevelt + testString: assert(myRegex.test('Eleanor Roosevelt'), 'Your regex myRegex should return true for the string Eleanor Roosevelt'); + - text: Your regex myRegex should return false for the string Franklin Rosevelt + testString: assert(!myRegex.test('Franklin Rosevelt'), 'Your regex myRegex should return false for the string Franklin Rosevelt'); + - text: You should use .test() to test the regex. + testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/), 'You should use .test() to test the regex.'); + - text: Your result should return true. + testString: assert(result === true, 'Your result should return true.'); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +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 +
+ +```js +let myString = "Eleanor Roosevelt"; +let myRegex = /(Franklin|Eleanor).*Roosevelt/; +let result = myRegex.test(myString); +``` +
diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters/index.md new file mode 100644 index 0000000000..f5808ebe1d --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters/index.md @@ -0,0 +1,24 @@ +--- +title: Check For Mixed Grouping of Characters +--- +## Check For Mixed Grouping of Characters + +### Hint 1 + +Use `a|b` to check for either `a` or `b`. + +### Hint 2 + +Your regex should use mixed grouping like `/P(engu|umpk)in/g`. + +### Hint 3 + +Use `.*` to allow for middle names. + +### Solution + +```javascript +let myString = "Eleanor Roosevelt"; +let myRegex = /(Franklin|Eleanor).*Roosevelt/; +let result = myRegex.test(myString); +``` \ No newline at end of file