Remove Punctuation With a Regular Expression
Learn how to use a regular expression to remove punctuation from text in JavaScript, Python, and other tools, with practical examples and checks.
Before you start
Removing punctuation with a regular expression is a text transformation that runs locally in whichever script, notebook, or editor you use. You need a source string and a regex engine that supports character classes and replacement. The examples below use JavaScript syntax because it is widely available, but the same pattern style works in Python, Java, and most other engines with minor quoting or method changes.
Decide first whether you want to keep letters, numbers, spaces, and selected symbols such as hyphens or apostrophes. A broad punctuation class can remove characters that matter in filenames, captions, or transcript text, so prepare a copy of the original string before cleaning.
Step-by-step instructions
Use a Unicode punctuation class
In modern JavaScript, use the Unicode property escape \p{P} with the u flag. This matches punctuation characters rather than trying to list every mark manually. The pattern removes punctuation while leaving letters, numbers, spaces, and line breaks unchanged.
- Store the original text in a variable such as transcript.
- Apply the replace method with the pattern /[\p{P}]/gu and an empty replacement string.
- Set the u flag so Unicode property escapes are recognized.
- Set the g flag so every match is removed instead of only the first one.
- Assign the result to a new variable so the original text remains available.
Use a custom character class for older engines
If your environment does not support \p{P}, build a character class with the punctuation you need to remove. A common JavaScript expression replaces any character that is not a word or whitespace by using the shorthand /[^\w\s]/g. This keeps letters, digits, the underscore, spaces, tabs, and line breaks while removing punctuation and other non-word characters.
The negation shorthand is compact but may also remove characters such as emoji or math symbols. If you want stricter control, list the punctuation marks explicitly in the class and escape characters that have special meaning in regex, such as the backslash, hyphen, or closing bracket.
Apply the same idea in Python
In Python, import the re module and call re.sub with the pattern, replacement, and source string. A version that removes common punctuation can use a character class such as [,.!?;:'"()[]{}@#%&*_-] with an empty replacement string. Python's re.sub returns a new string and does not change the original.
Clean tokens from a tokenizer
If you are using a tokenizer that produces punctuation tokens, apply the regex to each token after tokenization rather than expecting the tokenizer itself to drop punctuation. Some tokenizers expose an option for filtering punctuation, but support varies by version. Filtering the tokens yourself gives you the same result without relying on an option that may not exist.
Check the output
Compare the cleaned text against the original to confirm that only punctuation disappeared. Look for preserved spaces, correct word boundaries, and no accidental removal of letters, digits, or intended symbols. The cleaned string may contain double spaces where punctuation previously joined words, so normalize or trim spacing only if your downstream task requires it.
If you removed a custom set of characters, check examples that contain hyphens, apostrophes, periods, and quotes. A broad Unicode punctuation class may also remove decimal separators or hyphens in compound terms, which is expected for the pattern but may be unsuitable for your text.
Common problems and next steps
A recurring issue is removing only the first punctuation mark because the global flag is missing. Add g in JavaScript or confirm that the Python function is re.sub rather than a method that stops after one match. Another issue is keeping underscores when you use /[^\w\s]/g, since the word shorthand includes the underscore; remove the underscore if it is not wanted.
If punctuation removal changes meaning, such as turning a decimal into a whole number or joining separate sentences, use a narrower pattern that targets only the marks you can safely remove. For jQuery-based pages, use the same JavaScript replacement on the string before inserting it into the page; jQuery does not have a special punctuation method, so a regular expression remains the practical tool.
Test the expression on a short sample before processing large files. Once the cleaned text is acceptable, pass it to the next step such as search, token counting, or filename generation. Keep the original text intact until the result is verified.