admin管理员组文章数量:1400722
I'm stumped...
Why does the regex javascript string replace work for the string with special characters, but not the object converted to a string?
let regex = /[\t\r\n]/g
var directString = "{'name':{'first':' billy bob ','last':'\n\rjoe\tblow'}}"
console.log(directString.replace(regex, "a"));
//Output: "{'name':{'first':' billy bob ','last':'aajoeablow'}}"
let obj = {
name: {
first: " billy bob ",
last: "\n\rjoe\tblow"
},
}
let objAsString = JSON.stringify(obj);
let stringifiedString = objAsString.replace(regex, "a")
console.log(stringifiedString)
//Output: "{'name':{'first':' billy bob ','last':'\n\rjoe\tblow'}}"
//??? Why isn't \n\r and \t being replaced????
I'm stumped...
http://codepen.io/anon/pen/rjjGEE
Why does the regex javascript string replace work for the string with special characters, but not the object converted to a string?
let regex = /[\t\r\n]/g
var directString = "{'name':{'first':' billy bob ','last':'\n\rjoe\tblow'}}"
console.log(directString.replace(regex, "a"));
//Output: "{'name':{'first':' billy bob ','last':'aajoeablow'}}"
let obj = {
name: {
first: " billy bob ",
last: "\n\rjoe\tblow"
},
}
let objAsString = JSON.stringify(obj);
let stringifiedString = objAsString.replace(regex, "a")
console.log(stringifiedString)
//Output: "{'name':{'first':' billy bob ','last':'\n\rjoe\tblow'}}"
//??? Why isn't \n\r and \t being replaced????
Share
Improve this question
asked Jan 19, 2017 at 1:16
OrlandoOrlando
1471 silver badge11 bronze badges
1
-
You should log the values of
directString
andobjAsString
to the console. You will see that you have two different strings (and I'm not talking about the quotation marks). – Felix Kling Commented Jan 19, 2017 at 1:23
2 Answers
Reset to default 3When you use escapes like \n
and \t
in a string constant (like your first example), what you end up with is a string that contains the intended special characters (newline or tab).
When you JSON encode an object, however, what you end up with is a string that contains the escape sequences themselves, not the special characters. The work that JSON.stringify()
does has to include making sure that a subsequent parse of the JSON will re-create the original values of all the string-valued object properties. Thus it turns the embedded special characters in the strings back into escape sequences. Your regular expression is looking for the special characters, not the escape sequences.
Do not attempt to manipulate JSON strings with regexp. Pain will follow.
Instead, do this:
var jsonString = String.raw`{"a": "foo\nbar\rbaz\tqux"}`;
var replaced = JSON.stringify(JSON.parse(jsonString, (key, value) =>
typeof value === 'string' ? value.replace(/[\t\r\n]/g, '') : value
));
console.log(replaced);
本文标签: javascriptRegex replace of special characters in JSON string not workingStack Overflow
版权声明:本文标题:javascript - Regex replace of special characters in JSON string not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744192514a2594583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论