Sara Ree
Sara Ree

Reputation: 3543

remove all signs and marks in the a string except the apostrophe mark

I want to remove all signs and marks in the reference except the apostrophe mark.

So here is what I have so far:

let reference = "I was; sent* to? Earth,* to protect you. he's car: is! red."


let refered = reference.replace(/[^\w\s]/gi, '');
console.log(refered);

The problem is I can't make an exception for apostrophe mark and it'll be removed from the string.

Upvotes: 1

Views: 94

Answers (1)

Ismael Padilla
Ismael Padilla

Reputation: 5566

You can achieve this by escaping the apostrophe in your expression (you escape it by typing \'):

let reference = "I was; sent* to? Earth,* to protect you. he's car: is! red."


let refered = reference.replace(/[^\w\s\']/gi, '');
console.log(refered); // I was sent to Earth to protect you he's car is red

[^\w\s\'] means everything that isn't a word (\w), a space (\s) or an apostrophe (\').

Upvotes: 2

Related Questions