RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to find special characters

/[^A-Za-z0-9\s]/g

Rather than listing punctuation — a list you will always leave something out of — negate what you allow. This finds every character that is not an ASCII letter, digit or whitespace, which is what people mean by 'special character' in a validation rule.

What each part does

[^ … ]a negated class: any character not listed inside
A-Za-z0-9\sthe characters you consider ordinary
gthe global flag, so you get every occurrence rather than the first

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
hello!matches!
a@bmatches@
100%matches%
hellono match
abc 123no match

The mistake to avoid

Writing a positive list like [!@#$%^&*]. Users paste em dashes, curly quotes and non-breaking spaces, and every one of those slips through.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers