I'm stumped...
我很难过......
http://codepen.io/anon/pen/rjjGEE
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?
为什么正则表达式javascript字符串替换为具有特殊字符的字符串,但不是转换为字符串的对象?
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????
2 个解决方案
#1
4
When 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).
当你在字符串常量中使用\ n和\ t等转义符时(就像你的第一个例子一样),你最终得到的是一个包含预期特殊字符(换行符或制表符)的字符串。
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.
但是,当您对对象进行JSON编码时,您最终得到的是一个包含转义序列本身的字符串,而不是特殊字符。 JSON.stringify()所做的工作必须包括确保JSON的后续解析将重新创建所有字符串值对象属性的原始值。因此,它将字符串中嵌入的特殊字符转换为转义序列。您的正则表达式正在查找特殊字符,而不是转义序列。
#2
0
Do not attempt to manipulate JSON strings with regexp. Pain will follow.
不要尝试使用regexp操作JSON字符串。疼痛随之而来。
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);
#1
4
When 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).
当你在字符串常量中使用\ n和\ t等转义符时(就像你的第一个例子一样),你最终得到的是一个包含预期特殊字符(换行符或制表符)的字符串。
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.
但是,当您对对象进行JSON编码时,您最终得到的是一个包含转义序列本身的字符串,而不是特殊字符。 JSON.stringify()所做的工作必须包括确保JSON的后续解析将重新创建所有字符串值对象属性的原始值。因此,它将字符串中嵌入的特殊字符转换为转义序列。您的正则表达式正在查找特殊字符,而不是转义序列。
#2
0
Do not attempt to manipulate JSON strings with regexp. Pain will follow.
不要尝试使用regexp操作JSON字符串。疼痛随之而来。
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);