Jestus
Jestus

Reputation: 635

Javascript replace function will not remove quotes

I'm currently doing the following to remove extraneous characters and quotes from my strings:

console.log(word);
word = word.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "");
console.log(word);
word = word.replace(/["']/g, "");
console.log(word);

Many words are being scanned, but my output tends to be:
“If
“If
"If

OR

time,”
time”
time”

Is my regex wrong?

Upvotes: 0

Views: 94

Answers (1)

J. Titus
J. Titus

Reputation: 9690

I think the easiest way to solve this would be to use the following, instead:

word = word.replace(/[^\w\s]/g, '');

[^ ... ] - inverted selection

\w - matches alphanumerics, regardless of case

\s - matches spaces

Upvotes: 1

Related Questions