如何替换所有的 JavaScript中的JSON字符串中的兄弟姐妹?

时间:2022-06-12 16:51:37

How to replace all " " siblings from JSON string?

如何用JSON字符串替换所有的“ ”?

{"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"}

And btw, advice me please some good article or book from where I could finally learn Regexp :(

顺便说一句,请给我一些好的文章或书,让我最终能学到Regexp:(

4 个解决方案

#1


2  

var json = { "Cat"  : "laps_ milk",
             "Dog"  : "Woofs_ at_ Postman",
             "Bird" : "Jumps_ over_ the_ river",
             "I"    : "Want_ to_ learn_ Regexp" };
for (var prop in json) {
    json[prop] = json[prop].replace(/_/gi, '');
}

Regular Expressions is a good place to learn regexes.

正则表达式是学习正则表达式的好地方。

#2


4  

If you're parsing the JSON string, you can also use the reviver parameter of JSON.parse(string, [reviver]):

如果解析JSON字符串,还可以使用JSON的reviver参数。parse(string(兴奋剂)):

var jsonStr = '{"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"}';
var result = JSON.parse(jsonStr, function (key, value) {
    return value.replace(/ /g, " ");
});

Likewise, the stringify method allows a replacer function which will replace any values when converting to a JSON string:

同样,stringify方法允许替换函数在转换为JSON字符串时替换任何值:

var obj = {"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"};
var result = JSON.stringify(obj, function (key, value) {
    return value.replace(/ /g, " ");
});

Of course, this is assuming you're using json2.js or a browser with the correct ECMAScript 5th Edition implementation of the JSON object.

当然,这是假设你在使用json2。js或浏览器,使用正确的ECMAScript第5版实现JSON对象。

#3


1  

Try this:

试试这个:

var obj = {"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"};
for(var key in obj) {
    obj[key] = obj[key].replace(' ', '');
}

Also, the place that has helped me most in learning regular expressions:

此外,在学习正则表达式方面,帮助我最多的地方是:

#4


0  

In Mootools: console.log(JSON.encode(mystring).replace(/ /gi, ' '));

在Mootools中:console.log(JSON.encode(mystring).replace(/ /gi, '));

#1


2  

var json = { "Cat"  : "laps_ milk",
             "Dog"  : "Woofs_ at_ Postman",
             "Bird" : "Jumps_ over_ the_ river",
             "I"    : "Want_ to_ learn_ Regexp" };
for (var prop in json) {
    json[prop] = json[prop].replace(/_/gi, '');
}

Regular Expressions is a good place to learn regexes.

正则表达式是学习正则表达式的好地方。

#2


4  

If you're parsing the JSON string, you can also use the reviver parameter of JSON.parse(string, [reviver]):

如果解析JSON字符串,还可以使用JSON的reviver参数。parse(string(兴奋剂)):

var jsonStr = '{"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"}';
var result = JSON.parse(jsonStr, function (key, value) {
    return value.replace(/ /g, " ");
});

Likewise, the stringify method allows a replacer function which will replace any values when converting to a JSON string:

同样,stringify方法允许替换函数在转换为JSON字符串时替换任何值:

var obj = {"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"};
var result = JSON.stringify(obj, function (key, value) {
    return value.replace(/ /g, " ");
});

Of course, this is assuming you're using json2.js or a browser with the correct ECMAScript 5th Edition implementation of the JSON object.

当然,这是假设你在使用json2。js或浏览器,使用正确的ECMAScript第5版实现JSON对象。

#3


1  

Try this:

试试这个:

var obj = {"Cat":"laps milk","Dog":"Woofs at Postman","Bird":"Jumps over the river","I":"Want to learn Regexp"};
for(var key in obj) {
    obj[key] = obj[key].replace(' ', '');
}

Also, the place that has helped me most in learning regular expressions:

此外,在学习正则表达式方面,帮助我最多的地方是:

#4


0  

In Mootools: console.log(JSON.encode(mystring).replace(/ /gi, ' '));

在Mootools中:console.log(JSON.encode(mystring).replace(/ /gi, '));