-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_Matching_hard.js
More file actions
47 lines (36 loc) · 1.37 KB
/
Copy pathregex_Matching_hard.js
File metadata and controls
47 lines (36 loc) · 1.37 KB
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
43
44
45
46
47
// Implement regular expression matching with support for '.' and '*'.
// '.' Matches any single character.
// '*' Matches zero or more of the preceding element.
// The matching should cover the entire input string (not partial).
// The function prototype should be:
// bool isMatch(const char *s, const char *p)
// Some examples:
// isMatch("aa","a") → false
// isMatch("aa","aa") → true
// isMatch("aaa","aa") → false
// isMatch("aa", "a*") → true
// isMatch("aa", ".*") → true
// isMatch("ab", ".*") → true
// isMatch("aab", "c*a*b") → true
var isMatch = function(s, p) {
// var re = new RegExp("a|b", "g");
// same as
// var re = /a|b/g;
let result;
// creating regex from the string
let re = new RegExp( "^"+p+"$" , "" );
//console.log("s.match(re) ", s.match(re) );
if( s.match(re) === null ){
result = false;
} else if( s.match(re).length != 0 ){
result = true;
}
return result
};
// console.log( isMatch("aa","a") ); //→ false
// console.log( isMatch("aa","aa") ); //→ true
// console.log( isMatch("aaa","aa") ); //→ false
// console.log( isMatch("aa", "a*") ); //→ true
// console.log( isMatch("aa", ".*") ); //→ true
// console.log( isMatch("ab", ".*") ); //→ true
// console.log( isMatch("aab", "c*a*b") ); //→ true