JavaScript regular expression in Actions All In One

发布时间 2023-08-22 21:20:41作者: xgqfrms

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]

image

https://stackoverflow.com/a/76953550/5934465





(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://stackoverflow.com/questions/76952844/replace-words-in-string-with-values-of-string-array

https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!