1

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?

Zemprof
  • 245
  • 1
  • 2
  • 10

4 Answers4

3

You can use map function of Array's to create a new Array

var replaced = ['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html'].map(function(v) {
  return v.replace(/\\/g, '/');
});

console.log(replaced);
Bulkan
  • 2,555
  • 1
  • 20
  • 31
2

Since you mentioned node.js, you can just use .map:

var replaced = ['test\\test1\\test2\\myfile.html', 'test\\test1\\test2\\myfile2.html'].map(function (x) {
  return x.replace(/\\/g, '/');
});
Mrchief
  • 75,126
  • 20
  • 142
  • 189
0

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?

I think you can use the replace function of the String object.

For more reference, please go to:

http://www.w3schools.com/jsref/jsref_replace.asp

Hope that helps

Community
  • 1
  • 1
Ramon Araujo
  • 1,743
  • 22
  • 31
0
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"]

corkin
  • 81
  • 4