Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brunocoelho/564b54f37d6f3928c380351dd2df44e0 to your computer and use it in GitHub Desktop.
Save brunocoelho/564b54f37d6f3928c380351dd2df44e0 to your computer and use it in GitHub Desktop.
JavaScript Regular Expression Whitelist for String
// in the regex, the carrot(^) negates the match, so if the stringToReplace contains something that is not a-z or 0-9 it will be replaced
// In this case the desired string will only be alpha numeric characters, stripping out spaces and symbols
// Help from: http://stackoverflow.com/questions/4374822/javascript-regexp-remove-all-special-characters
var desired = stringToReplace.replace(/[^a-zA-Z0-9]/gi, '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment