Skip to content

Instantly share code, notes, and snippets.

@junminstorage
Created August 25, 2017 04:12
regular expression
function regEx(p, w, i, j) {
if( i == p.length && j == w.length )
return true;
if ( j == w.length || i == p.length )
return false;
//if second char is *
if ( i+1<p.length && p.charAt(i+1) == '*' ){
//match pre char 0 time
if(regEx(p, w, i+2, j))
return true;
//match pre char 1+ times
for(let k = j; k<w.length && (p.charAt(i)==w.charAt(k) || p.charAt(i) == '.'); k++) {
if(regEx(p, w, i+2, k+1))
return true;
}
}
if(p.charAt(i) == w.charAt(j) || p.charAt(i) == '.') {
if(regEx(p, w, i+1, j+1))
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment