回圈遍歷字串陣列并使用 RegExp 替換值的最佳方法是什么?陣列如下所示:
['./content1/App.js', './sample2/App.js', './exam/App.js']
我需要用值生成新陣列:
['content1', 'sample2', 'exam']
uj5u.com熱心網友回復:
如果問題中的資料項模式相同,那么您可以嘗試
const arr = ["./content1/App.js", "./sample2/App.js", "./exam/App.js"];
const result = arr.map((s) => s.match(/\.\/([^\/] )/)[1]);
console.log(result);
uj5u.com熱心網友回復:
使用 Array 原型的 map 方法可以實作這一點。
let newArray =['./content1/App.js', './sample2/App.js', './exam/App.js'].map((string)=>{
// code to replace string
//return the modified string
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362421.html
標籤:javascript 正则表达式
下一篇:sed用正則運算式代替
