Remove Characters with a Regex
This workflow opens [^A-Za-z0-9] with g and an empty replacement: Order #A-42! becomes OrderA42. The example removes spaces and all non-ASCII letters as well as punctuation, so change the rule to match what you intend to keep. Use \s+ and a single space to collapse whitespace, or a narrower character class to remove only specified symbols.
$& keeps the match, $1 to $99 keep numbered groups, $<name> keeps a named group, and $$ inserts a dollar sign. Type an actual newline for a line break. Without g only the first match is replaced.
Matching.
Replaced text
Replacing.
- Matches
- -
- Groups
- -
- Flags
- Engine
- JavaScript
Matching.
d record where each match starts and ends · g find every match, not only the first · i ignore upper and lower case · m ^ and $ match at every line · s a dot also matches a newline · u read the text as unicode code points · v unicode sets, with set operations in classes · y match only where the last one left off
- both, with offsets
- listed by name
- stopped by timeout
Common questions
- How do I keep spaces?
- Add a literal space inside the negated character class: [^A-Za-z0-9 ] keeps ASCII letters, digits and ordinary spaces. It still removes tabs, line breaks and non-ASCII letters.
- Can I preserve Unicode letters and numbers?
- Use [^\p{L}\p{N}] with gu in a browser supporting Unicode property escapes. This keeps Unicode letters and numbers; combining marks require an additional \p{M} if your rule needs them.
- Does stripping HTML tags sanitize HTML?
- No. Regex character removal is not an HTML parser or a sanitizer. Nested markup, quoted attributes and unsafe content need a dedicated HTML workflow.
Matching uses your browser's own JavaScript regular expression engine, so what you see here is what your code will do. It runs in a disposable worker with a timeout, so a pattern that would hang the page is stopped and reported instead.