user2558534
user2558534

Reputation: 39

regex in javascript

Hey there how do I escape ' and " using javascript regex?

because I want

Annie said, "It's really funny."

to be like,

Annie said, \"It\'s really funny.\"

Upvotes: 0

Views: 35

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Use string.replace.

string.replace(/(['"])/g, "\\$1")

Example:

> var s = 'Annie said, "It\'s really funny."'
undefined
> console.log(s.replace(/(['"])/g, "\\$1"))
Annie said, \"It\'s really funny.\"

Upvotes: 1

Related Questions