Reputation: 6896
I have the following JavaScript line of code that removes characters- including commas- from a string:
return str.replace(/(?!\/)(?!\ )(?!\-)(\W)/ig, '');
How can I only take out the piece of code that removes the commas?
Upvotes: 1
Views: 3841
Reputation: 91385
The regex /(?!\/)(?!\ )(?!\-)(\W)/ig
matches any character that is not a "word" character (ie. [a-zA-Z0-9_]
) and the 3 lookaheads restrict also the character /
, and
-
. The comma is removed because it is part of \W
.
If you want to keep it, add another lookahead: (?!,)
, then your regex becomes:
return str.replace(/(?!\/)(?! )(?!-)(?!,)(\W)/g, '');
I've removed the unnecessay escape and the case insensitive flag.
This should be written as:
return str.replace(/[^\w\/, -]/g, '');
Upvotes: 1