JavaScript regular expression in Actions All In One
JavaScript 正则表达式实战
demos
在字符串中匹配多组数据
const str = 'lines[0][config][options][343]';
const reg = /\[([0-9]+|[a-z]+|[A-Z]+)\]/g;
const groups = [];
str.replaceAll(reg, group => {
let value = group.replace(`[`, ``).replace(`]`, ``)
groups.push(value)
return ''
});
console.log(`groups =`, groups);
// ['0', 'config', 'options', '343']
// one more thing ✅
const arr = groups.map(item => isNaN(item) ? item : parseInt(item))
console.log(`arr =`, arr);
// [0, 'config', 'options', 343]
https://stackoverflow.com/a/76953550/5934465