首页 > 其他 > 详细

通过正则查找指定内容

时间:2020-01-24 15:36:15      阅读:91      评论:0      收藏:0      [点我收藏+]
let str = `"foo" and "bar" and "baz"`

//方法一
function select (regExp, str) {
  const matches = []
  while (true) {
    const match = regExp.exec(str)
    if(match === null) break
    matches.push(match[1])
  }
  return matches
}

console.log(select(/"([^"]*)"/g,str))

//方法二
console.log(str.match(/"([^"]*)"/))

//方法三
function select (regExp, str) {
  const matches = []
  str.replace(regExp,function (all, first) {
     matches.push(first)
  })
  return matches
}
console.log(select(/"([^"]*)"/g,str))

//es10 方法四:matchAll
 function select (regExp, str) { const matches = [] for (const match of str.matchAll(regExp)) { matches.push(match[1]) } } console.log(select(/"([^"]*)"/g,str))

 

通过正则查找指定内容

原文:https://www.cnblogs.com/qjb2404/p/12232246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!