Regex to Match Strings Without Special Characters
Validate strings without special characters using this regex. Allows only letters, digits, and whitespace. Free online regex tester.
Regular Expression
/^[a-zA-Z0-9\s]+$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| [a-zA-Z0-9\s] | Character class — matches any one of: a-zA-Z0-9\s |
| + | Matches the preceding element one or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates that a string contains only letters, digits, and whitespace characters (no special characters). Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
[a-zA-Z0-9\s]+ — A character class matching one or more allowed characters. The ranges include: a-z for lowercase letters, A-Z for uppercase letters, 0-9 for digits, and \s for whitespace characters (spaces, tabs, newlines, etc.). The + quantifier requires at least one character.
$ — Anchors the match at the end of the string.
This pattern is a slightly relaxed version of the alphanumeric pattern that also permits spaces and other whitespace. It rejects any special characters including punctuation marks, symbols, and control characters (except whitespace).
Common use cases include validating names (where spaces are needed between first and last names), descriptions that should not contain special characters, and sanitizing user input to prevent injection attacks. For example, 'John Smith' and 'Room 101' would match, but 'hello@world' and 'price: $50' would not.
Like the alphanumeric pattern, this is ASCII-only and does not handle international characters. For multilingual support, consider using Unicode property escapes or language-specific character ranges. The \s shorthand matches more than just spaces: it also includes tabs, carriage returns, line feeds, and other Unicode whitespace characters.
Example Test Strings
| Input | Expected |
|---|---|
| Hello World | Match |
| Room 101 | Match |
| hello@world | No Match |
| no-hyphens | No Match |
| Simple Text 123 | Match |
Try It — Interactive Tester
16 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex