what's the best way to loop through array of strings and replace values using regex? Array looks like this:
['./content1/App.js', './sample2/App.js', './exam/App.js']
i need to generate new array with values:
['content1', 'sample2', 'exam']
You can make use of regex here /(?<=\.\/)[^\/]+/
const arr = ["./content1/App.js", "./sample2/App.js", "./exam/App.js"]; const result = arr.map((s) => s.match(/(?<=\.\/)[^\/]+/)[0]); console.log(result);
If the data item pattern is same then you can try:
var data = ['./content1/App.js', './sample2/App.js', './exam/App.js']; var res = data.map(i => i.split('/')[1]); console.log(res);
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 })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.