0

Please help me how to pass a string(which i cannot edit, like coming from DB) with double quotes to a method in the javascript.

*Please note this "Hello "world", Welcome..." is coming from DB which i cannot edit. To understand you easily how the format is coming i placed it here like this

var tempString = "Hello "world", Welcome...";
sampleMethod(tempString);
sampleMethod : function(tempString)
{
  &nbsp&nbsp&nbsp&nbspalert(tempString)<br>
}

Actually i was working on this thing in the EXtjs 3.4 Grouping View for Grouping Store.

Pelase help me with this.

Bug
  • 2,576
  • 2
  • 21
  • 36
Gokul Potluri
  • 262
  • 4
  • 16
  • wrap it in single quotes ? – adeneo Nov 19 '13 at 12:35
  • perhaps you should explain what happens when you try to pass the string to your js function? – Swifty Nov 19 '13 at 12:35
  • 1
    Can you show us the code that receives this string? – Halcyon Nov 19 '13 at 12:35
  • @Swifty add explained in the question i was doing this in the extjs. I am passing object directly through javascript code to extjs method, it is taking the object as "[object Object]" in the parameter. This is giving me the error. – Gokul Potluri Nov 19 '13 at 12:43
  • I guess Frits wants to see the actual code, where you create this object, since if you can read something to a JS variable, you are supposed to able to edit it too. – Teemu Nov 19 '13 at 13:17

3 Answers3

0

You should use single quotes to wrap the string.

var tempString = 'Hello "world", Welcome...';

Or you could escape the inner double quotes with &#34; (Depending on the use of the string)

Or you could regular expressions to change the string from the database. Read this

Community
  • 1
  • 1
Arno
  • 161
  • 10
  • "`Please note this "Hello "world", Welcome..." is coming from DB which i cannot edit.`". – Teemu Nov 19 '13 at 12:37
0

Use \ to escape the quotes

var tempString = "Hello \"world\", Welcome...";

DEMO If the string in the DB can't be changed manually. Use single quotes instead.

var tempString = 'Hello "world", Welcome...';
softvar
  • 17,917
  • 12
  • 55
  • 76
0

One possible way to change this specific string:

var tempString = "Hello \"world\", Welcome...";

var result = tempString.replace(/([^"]*)\"(\w*)\"([^"]*)/, "$1\'$2\'$3")

result should be "Hello 'world', Welcome...".

So you can call method

sampleMethod(tempString.replace(/([^"]*)\"(\w*)\"([^"]*)/, "$1\'$2\'$3"));

And if there is space in the world:

var tempString = "Hello \"wo r ld\", Welcome...";

this replacement is better:

var result = tempString.replace(/([^"]*)\"([\w\s]*)\"([^"]*)/, "$1\'$2\'$3")
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
  • BTW, this solution is adopted from [Regex for single quote within single quotes](http://stackoverflow.com/questions/18672629/regex-for-single-quote-within-single-quotes) – Anto Jurković Nov 19 '13 at 13:44
  • 1
    Even though your solution is helping me upto 100%, with the reference of your solution i made my thing work. Thank you – Gokul Potluri Nov 21 '13 at 10:29