0

I have asked this question before and it's really a simple question but I am still waiting for an answer.

I want string containing "'" single quote.

I know I can use double single quote to evade the problem but I don't want to use double single quotes.

Is there any solution I can use single quote in Javascript. I am saving value with single quote in cookies and operating on it in javascript.

Its really a simple functionality I don't know why the developer community do not know some simple solution to it

UPDATE: I don't want to use back slash since I don't have access to string source

user786
  • 3,902
  • 4
  • 40
  • 72
  • 3
    please add some example code... What are you trying to do, what error you get and what you are expecting it to do? It is hard to answer just like that. – Salketer May 11 '15 at 12:33
  • 1
    You can escape `'` like `\'` – Satpal May 11 '15 at 12:33
  • did You try the escape character: var x="bla bla \" bla"; – MoLow May 11 '15 at 12:33
  • just as simple for you to do some research on the web – charlietfl May 11 '15 at 12:35
  • I do not have access to value source. I can not put back slash before every single quote. there gotta be some other solution – user786 May 11 '15 at 12:41
  • The following works fine in "modern" browsers ... please add an example how you operate on strings ......: var testStr = "That's a valid string"; alert(testStr); var testStr = "'"; alert(testStr); –  May 11 '15 at 12:58
  • @slash4 Exactly like this. I know we can embed javascript in html. So I want similar solution. – user786 May 11 '15 at 13:13
  • @james-donnelly I have updated my question. It's not a duplication. I reject the solution offered in that question – user786 May 11 '15 at 13:14
  • @Alex one of the answers on that question answers that: http://stackoverflow.com/a/6820024/1317805 (read the comments though). – James Donnelly May 11 '15 at 13:26
  • @JamesDonnelly encodeURI will give me "St.%20John" 's instead of St. John''s. – user786 May 11 '15 at 15:36

1 Answers1

4

You can use the \ escape character:

This is valid code:

var testString = 'this could\'ve gone wrong, but it didn\'t';

You can replace them yourself with this line:

strInputString = strInputString.replace(/'/g, "\\'");

This will escape all existing single quotes for you.

Jordumus
  • 2,763
  • 2
  • 21
  • 38