-
Notifications
You must be signed in to change notification settings - Fork 183
/
20 - Day 7 - Regular Expressions I.js
42 lines (34 loc) · 1.08 KB
/
20 - Day 7 - Regular Expressions I.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/js10-regexp-1/problem
// Difficulty: Easy
// Max Score: 15
// Language: JavaScript (Node.js)
// ========================
// Solution
// ========================
function regexVar() {
// Declare a RegExp object variable named 're'
// It must match a string that starts and ends with the same vowel (i.e., {a, e, i, o, u})
var re = RegExp(/^([aeiou]).*\1$/);
// Do not remove the return statement
return re;
}
function main() {
const re = regexVar();
const s = readLine();
console.log(re.test(s));
}
// ========================
// Explanation
// ========================
/*
- ^ => matches only at the start (0th index):
- () => stores matching value captured within
- [aeiou] => matches any of the characters in the brackets
- . => matches any character:
- + => for 1 or more occurrances (this ensures str length > 3)
- \1 => matches to previously stored match.
- $ ensures that matched item is at end of the sequence
*/