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
3 Answers
Reset to default 2use \\ 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
版权声明:本文标题:jquery - How to escape Backslash in Javascript object literal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744887346a2630565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论