Reputation: 39
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
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