What's the best way to loop through array of strings and replace values using RegExp? Array looks like this:
['./content1/App.js', './sample2/App.js', './exam/App.js']
I need to generate new array with values:
['content1', 'sample2', 'exam']
CodePudding user response:
If the data item pattern is same in the question then you can try
const arr = ["./content1/App.js", "./sample2/App.js", "./exam/App.js"];
const result = arr.map((s) => s.match(/\.\/([^\/] )/)[1]);
console.log(result);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Using the map method of the Array prototype this can be achieved.
let newArray =['./content1/App.js', './sample2/App.js', './exam/App.js'].map((string)=>{
// code to replace string
//return the modified string
})
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>