[英]Nodejs: How to replace certain characters within an array with other characters in javascript
說我有一個像這樣的數組:
['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html']
我只想用“ /”替換所有“ \\ \\”字符並將其存儲到新數組中,以便新數組看起來像這樣:
['test/test1/test2/myfile.html', 'test/test1/test2/myfile2.html']
我該怎么做呢?
您可以使用Array的map函數創建一個新的Array
var replaced = ['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html'].map(function(v) {
return v.replace(/\\/g, '/');
});
console.log(replaced);
既然您提到了node.js,就可以使用.map :
var replaced = ['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html'].map(function (x) {
return x.replace(/\\/g, '/');
});
首先,您必須使用任何迭代方法遍歷數組。
這將幫助您:
我認為您可以使用String對象的replace函數。
有關更多參考,請訪問:
http://www.w3schools.com/jsref/jsref_replace.asp
希望能有所幫助
var test = ['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html'];
for(var i=0;i<test.length;i++) {
test[i] = test[i].replace(/\\/g,'/');
}
console.log(test);
輸出[“ test / test1 / test2 / myfile.html”,“ test / test1 / test2 / myfile2.html”]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.