Skip to content Skip to sidebar Skip to footer

Escaping Quotes From Strings Using Javascript?

For instance say I have the string: var name = 'Mc'Obrian' I want to be able to escape the the first quote and the last quote only, not the quote used within the name, how can I a

Solution 1:

The following will properly escape the single quote given your current code:

var name = 'Mc\'Obrian'

I would encourage you to read the following tutorial about strings. If you are interested in performance considerations, read this question.

Solution 2:

use \ to escape the single quote like so:

var name = 'Mc\'Obrian'

Solution 3:

var name = 'Mc\'Obrian'

or

var name = "Mc'Obrian"

Post a Comment for "Escaping Quotes From Strings Using Javascript?"