我有一个简单的JSON,我想从一个属性中替换一个字符串。让我们说这是我的JSON:
var values = [
{ id: 10, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.256248, latitude: 19347774275 },
{ id: 11, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.7534025, latitude: 19347868 },
{ id: 12, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.85458, latitude: 1934748775 },
{ id: 13, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.5525, latitude: 19317868 }
]
现在我想做的是用斜杠\
替换每个/
。例如:
来自网址:www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG
到网址:www.mydomainname.com/images/uploads/xfolder/FILE_NAME.JPG
我尝试的很简单。这就是我试过的:
for(i = 0; i < values.length; i++){
var str = values[i].url;
var res = str.replace("''\''", "/");
}
但它没有用。任何帮助都会受到高度赞赏,因为已经有一段时间我被困住了提前谢谢:)
答案 0 :(得分:5)
您目前从未对res
做任何事情。请使用带有全局标志的正则表达式:
var values=[{id:10,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.256248,latitude:19347774275},{id:11,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.7534025,latitude:19347868},{id:12,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.85458,latitude:1934748775},{id:13,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.5525,latitude:19317868}];
values.forEach(value => value.url = value.url.replace(/\\/g, '/'));
console.log(values);
答案 1 :(得分:1)
for(var i=0; i < arr.length; i++) {
arr[i] = arr[i].replace(/\\/g, "/");
}