Reputation: 5188
I'm not great at regex but I have this for removing punctuation from a string.
let text = 'a user provided string'
let pattern = /(-?\d+(?:[.,]\d+)*)|[-.,()&$#![\]{}"']+/g;
text.replace(pattern, "$1");
I am looking for a way to modify this so that it keeps punctuation if inside a word e.g.
should all keep the punctuation. How would I modify it for that?
Upvotes: 0
Views: 165
Reputation: 163277
One option could be changing the \d
to \w
to extend the match to word characters and add a hyphen to the character class in the capturing group.
In the replacement use group 1.
(\w+(?:[.,-]\w+)*)|[-.,()&$#![\]{}"']+
If you want to match multiple hyphens, commas or dots you could repeat the character class [.,-]+
Upvotes: 1