Created
September 27, 2016 18:38
-
-
Save slickplaid/73926fcd2da6cd9ac7fa0dcf0bf435aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function buildRegexp(str) { | |
// start the regex with the | |
// ^ start key | |
let src = '^' + str; | |
// replace spaces with regex | |
// that accepts one or more spaces | |
// which will handle formatting | |
// inconsistencies | |
src = src.replace(/\s+/g, '\\s+'); | |
// reserved regex characters | |
// (finite list, not complete | |
// for this example) | |
src = src.replace('.', '\\.'); | |
// end with the regex $ end key | |
src += '$'; | |
return new RegExp(src); | |
} | |
function compare(src, str) { | |
let regex = buildRegexp(src); | |
// trim beginning and ending whitespace | |
str = str.trim(); | |
// log out so I can demo this | |
console.log(regex, str); | |
return regex.test(str); | |
} | |
compare('Hello, my name is Evan.', ' Hello, my name is Evan. '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment