Although Javascript does not support possessive quantifier like \w{2,}+
, your pattern may not give the desired results after replacement.
Remember that a space ( )
does not need to be within parenthesis to create a group. You must escape the dot \.
to match it literally, and {1,}
can be represented as +
.
You could repeat matching 2 or more word characters followed by a dot and 1 or more spaces OR match 1 or more non-word characters except for whitespace characters.
In the replacement, simply use an empty string.
(?:\w{2,}\.[^\S\n]+)+|[^\w\s]+
Check out this regex 101 demonstration.
const regex = /(?:\w{2,}\.[^\S\n]+)+|[^\w\s]+/g;
const s = `DR. Tida.
prof. Sina.
DR. prof. Tida.`;
console.log(s.replace(regex, ""));
Keep in mind that this part \w{2,}\.
might match more than just titles.
To make it more specific, you may list what you want to remove using alternation:
(?:\b(?:DR|prof)\.[^\S\n]+)+|[^\w\s]+
Here's another demo on regex101