Regex for no special characters (alphanumeric only)
This pattern accepts letters, digits and spaces and rejects everything else, such as @ # $ % & * and punctuation. Use it to validate a field, or flip the character class to find and remove special characters.
The pattern
/^[A-Za-z0-9 ]+$/gmThe tester opens with this pattern and the examples below already loaded. Nothing you type is uploaded.
How it works
^ … $- The whole value must fit the pattern, start to end.
A-Za-z- Uppercase and lowercase English letters.
0-9- Digits.
(space)- A literal space. Remove it from the class to forbid spaces too.
+- One or more of those characters.
Examples
Matches
Hello Worldabc123Order 66
Does not match
hello@worlda_bprice: $5no-dashes
Use it in JavaScript
const valid = /^[A-Za-z0-9 ]+$/;
valid.test('Hello World'); // true
valid.test('hello@world'); // false
// Strip special characters instead of rejecting them
const cleaned = text.replace(/[^A-Za-z0-9 ]/g, '');Limits and tips
- To remove special characters instead of rejecting them, replace the negated class: text.replace(/[^A-Za-z0-9 ]/g, "").
- Underscore and hyphen count as special here. Add them inside the brackets if you want to allow them.
- This rejects accented and non-English letters too. Use \p{L}\p{N} with the u flag if your users write in other languages.
Frequently asked questions
How do I remove special characters from a string in JavaScript?
Use str.replace(/[^A-Za-z0-9 ]/g, ""). The ^ inside the brackets negates the class, so every character that is not a letter, digit or space is deleted.
Which characters count as special?
Anything outside the allowed class: punctuation, symbols, emoji and, with this pattern, accented letters.