Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active October 15, 2022 18:59
Show Gist options
  • Save steveosoule/4495982 to your computer and use it in GitHub Desktop.
Save steveosoule/4495982 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, '')
@onderaltintas
Copy link

A real hero!

@jomla97
Copy link

jomla97 commented Sep 23, 2020

Thank you!

@adrianpauly
Copy link

It's "caret" btw :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment