admin管理员组

文章数量:1405154

I know question is asked many times, i gone through with all the questions. but none of them is helping my situation. I have a object literal, for one of the property i assign some string which has backslash charecter, but while reading the property the backslash is getting truncated.

I want to use this object literal in JSON.Stringify method. as per JSON, backslash are not allowed. we need to escape it. is there any way ?

i cant modify the data to add an extra backslash, because data is ing from the server. 'localhost\sqlserver'. after that i need to replace it.

Object literal

var data={
    s:'localhost\sqlserver'
}


function replaceToUpper(value) { 
    return   value.replace(/\\n/g, "\\n") 
                                      .replace(/\\'/g, "\\'") 
                                      .replace(/\\&/g, "\\&") 
                                      .replace(/\\r/g, "\\r") 
                                      .replace(/\\t/g, "\\t") 
                                      .replace(/\\b/g, "\\b") 
                                      .replace(/\\f/g, "\\f"); 
}

//reading the data

alert(replaceToUpper(data.s));

attempt 2 :

var values =JSON.stringify(data); // here that backslash getting trucunated 

what do i missing here. check the fiddle Object Literal with Backslash

i cant modify the data to add an extra backslash, because data is ing from the server. 'localhost\sqlserver'. after that i need to replace it. I know adding extra blackslash would help me. but i cant modify the data.

I know question is asked many times, i gone through with all the questions. but none of them is helping my situation. I have a object literal, for one of the property i assign some string which has backslash charecter, but while reading the property the backslash is getting truncated.

I want to use this object literal in JSON.Stringify method. as per JSON, backslash are not allowed. we need to escape it. is there any way ?

i cant modify the data to add an extra backslash, because data is ing from the server. 'localhost\sqlserver'. after that i need to replace it.

Object literal

var data={
    s:'localhost\sqlserver'
}


function replaceToUpper(value) { 
    return   value.replace(/\\n/g, "\\n") 
                                      .replace(/\\'/g, "\\'") 
                                      .replace(/\\&/g, "\\&") 
                                      .replace(/\\r/g, "\\r") 
                                      .replace(/\\t/g, "\\t") 
                                      .replace(/\\b/g, "\\b") 
                                      .replace(/\\f/g, "\\f"); 
}

//reading the data

alert(replaceToUpper(data.s));

attempt 2 :

var values =JSON.stringify(data); // here that backslash getting trucunated 

what do i missing here. check the fiddle Object Literal with Backslash

i cant modify the data to add an extra backslash, because data is ing from the server. 'localhost\sqlserver'. after that i need to replace it. I know adding extra blackslash would help me. but i cant modify the data.

Share Improve this question edited Nov 26, 2013 at 2:48 Ravi Gadag asked Nov 26, 2013 at 2:37 Ravi GadagRavi Gadag 15.9k5 gold badges59 silver badges85 bronze badges 2
  • i just concentrating the backslash escaping. i don't know why it's not working – Ravi Gadag Commented Nov 26, 2013 at 2:43
  • This question appears to be off-topic because i have changed the data layer such that it escapes the data. as question does not help any other people. pls close it – Ravi Gadag Commented Jan 22, 2014 at 4:28
Add a ment  | 

3 Answers 3

Reset to default 2

use \\ to escape \

var data = { s: 'localhost\\sqlserver'}

JSON.parse(JSON.stringify(data))

it is a string literal where \ is an escape character so if you want to use a \ as it is then you need to escape it with another \ like \\

'localhost\\sqlserver'

Demo: Fiddle

before save JSON as string into db or somethis use REGEX

JSON.stringify(drivers).replace(/"/g, '\\"')

本文标签: jqueryHow to escape Backslash in Javascript object literalStack Overflow