-1

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 achieve this in javascript? thanks

unknown
  • 846
  • 3
  • 15
  • 38

3 Answers3

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.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

use \ to escape the single quote like so:

var name = 'Mc\'Obrian'
Mark Walters
  • 12,060
  • 6
  • 33
  • 48
1

var name = 'Mc\'Obrian'

or

var name = "Mc'Obrian"

Darcy
  • 5,228
  • 12
  • 53
  • 79