Skip to content

Instantly share code, notes, and snippets.

@slickplaid
Created September 27, 2016 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slickplaid/73926fcd2da6cd9ac7fa0dcf0bf435aa to your computer and use it in GitHub Desktop.
Save slickplaid/73926fcd2da6cd9ac7fa0dcf0bf435aa to your computer and use it in GitHub Desktop.
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