[英]Nodejs: How to replace certain characters within an array with other characters in javascript
Say I have an array like this: 说我有一个像这样的数组:
['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html']
I just want to replace all of the "\\ \\" characters with "/" and store this into a new array so that the new array should look like this: 我只想用“ /”替换所有“ \\ \\”字符并将其存储到新数组中,以便新数组看起来像这样:
['test/test1/test2/myfile.html', 'test/test1/test2/myfile2.html']
How could I go about doing this? 我该怎么做呢?
First of all you have to traverse the array using any iteration method. 首先,您必须使用任何迭代方法遍历数组。
This will help you with that: 这将帮助您:
For-each over an array in JavaScript? 在JavaScript中逐个遍历数组?
I think you can use the replace function of the String object. 我认为您可以使用String对象的replace函数。
For more reference, please go to: 有关更多参考,请访问:
http://www.w3schools.com/jsref/jsref_replace.asp http://www.w3schools.com/jsref/jsref_replace.asp
Hope that helps 希望能有所帮助
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);
outputs ["test/test1/test2/myfile.html", "test/test1/test2/myfile2.html"] 输出[“ test / test1 / test2 / myfile.html”,“ test / test1 / test2 / myfile2.html”]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.