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?
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?
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, ''); \W from the regex? Could you also explain what you mean by: I've removed the unnecessary escape and the case insensitive flag.? Thank you!\W because it rests only 3 negative lookahead that are 0-length match, you can't remove nothing by nothing!