-2

I'm looking for a regex to detect special characters in javascript.

current attempt doesnt cater for all special characters:

        this.validateSpecialCharacters = (valueA): boolean => {
            const regex = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/;
            return regex.test(valueA);
        };

Does anyone have a better regex to cater for all english keyboard special characters e.g. @!"£$%^&*() etc

j08691
  • 204,283
  • 31
  • 260
  • 272
AngularM
  • 15,982
  • 28
  • 94
  • 169
  • 2
    What if you just did a Regex for *not alpha-numeric*, rather than trying to list out all special characters? [http://stackoverflow.com/a/6053606/2026606](http://stackoverflow.com/a/6053606/2026606) – Tyler Roper Oct 05 '16 at 15:04
  • Define "special". Punctuation? Non-alphanumeric? Not-used-in-everyday-conversation? "What's this called?" – deceze Oct 05 '16 at 15:05

1 Answers1

1

Since you are matching every special character, it's better to just tell the regex to not match alphanumeric characters.

const regex = /[^A-Za-z0-9]/

You can add any other cases inside the []. This regex will match anything that is not included inside the [].

davidhu
  • 9,523
  • 6
  • 32
  • 53