最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】JS6中的正则表达式去实现字符串匹配和提取

JS crifan 638浏览 0评论

ReactJS中,要用JS6代码去实现字符串的匹配和提取:

  //extarct device code from scanned string
  //www.ucows.cn/qr?id=88888888888888
  //12345678901234
  parseDeviceCode(){
    let scannedString = "www.ucows.cn/qr?id=88888888888888";
    // let scannedString = "12345678901234";
    console.log(`scannedString=${scannedString}`);
  }

js 正则匹配

ES6 正则匹配

JS正则表达式一条龙讲解,从原理和语法到JS正则、ES6正则扩展,最后再到正则实践思路 – wildWeb – SegmentFault

es6正则表达式 – IT-nose – 博客园

后 ES6 时代的正则匹配 – 众成翻译

ES6 正则的扩展 – ES6文档

然后用:

  //extarct device code from scanned string
  //www.ucows.cn/qr?id=88888888888888
  //12345678901234
  parseDeviceCode(){
    let deviceCode = null;
    let scannedString = "www.ucows.cn/qr?id=88888888888888";
    // let scannedString = "12345678901234";
    console.log(`scannedString=${scannedString}`);
    let deviceCodePattern = new RegExp(/(\d{14})/ig, ‘i’);
    console.log(deviceCodePattern);
    ///(\d{14})/i
    // let deviceCodeMatch = scannedString.match(deviceCodePattern);
    let deviceCodeMatch = deviceCodePattern.exec(scannedString);
    console.log(deviceCodeMatch);
    //["88888888888888", "88888888888888", index: 19, input: "www.ucows.cn/qr?id=88888888888888"]
    if (deviceCodeMatch) {
      deviceCode = deviceCodeMatch[0];
      console.log(deviceCode); //88888888888888
      console.log(deviceCodeMatch[1]); //88888888888888
    }
    return deviceCode;
  }

输出:

 

对于此处的输出中,match[0] match[1] 都是此处 匹配到的第一个括号里面的内容,有点疑惑

所以想要去搞清楚,此处的

pattern.exec()

后得到的match对象?

是什么东西?

ES6 regexp match object

RegExp.prototype.exec() – JavaScript | MDN

String.prototype.match() – JavaScript | MDN

“Return value

An Array containing the entire match result and any parentheses-captured matched results; null if there were no matches.

DescriptionEDIT

If the regular expression does not include the g flag, str.match() will return the same result as RegExp.exec(). The returned Array has an extra input property, which contains the original string that was parsed. In addition, it has an index property, which represents the zero-based index of the match in the string.

If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned. If there were no matches, the method returns null.”

【总结】

最后暂时用下面代码,实现了提取对应内容:

    // let scannedString = "www.ucows.cn/qr?id=88888888888888";
    // let scannedString = "12345678901234";
    console.log(`scannedString=${scannedString}`);
    let deviceCode = extractSingleStr(scannedString, /(\d{14})/ig);
    return deviceCode;
export function  extractSingleStr(curStr, pattern, flags=’i’) {
  let extractedStr = null;
  let rePattern = new RegExp(pattern, flags);
  console.log(rePattern);
  let matches = rePattern.exec(curStr);
  console.log(matches);
  if (matches) {
    extractedStr = matches[0];
    console.log(extractedStr);
  }
  console.log(`curStr=${curStr}, pattern=${pattern}, flags=${flags} -> extractedStr=${extractedStr}`);
  return extractedStr;
}

转载请注明:在路上 » 【已解决】JS6中的正则表达式去实现字符串匹配和提取

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
13 queries in 0.208 seconds, using 10.28MB memory