正则表达式有时返回结果,有时在类似的字符串上返回null

时间:2017-10-21 19:10:52

标签: javascript regex excel

我有以下示例代码,其中包含一系列Excel公式,我使用正则表达式试图将URL和超链接的标题从字符串中隔离出来。这似乎适用于某些人,但会在其他人身上返回null

我认为它可能是循环,但我也得到了不好的结果,如下面的例2所示。

示例1:

var linkList = ['=HYPERLINK("https://example.com","Example")', '=HYPERLINK("https://example2.com","Example 2")', '=HYPERLINK("https://example3.com","Example 3")', '=HYPERLINK("https://example4.com","Example 4")'];

const url_re = /".*?"/g;
const linkName_re = /\,(".*?")/g;

linkList.forEach(function(currentValue, index, array){
  +console.log(url_re.exec(currentValue));
  console.log(linkName_re.exec(currentValue));
  console.log("---------------------------");
});

输出:

[ '"https://example.com"',
  index: 11,
  input: '=HYPERLINK("https://example.com","Example")' ]
[ ',"Example"',
  '"Example"',
  index: 32,
  input: '=HYPERLINK("https://example.com","Example")' ]
---------------------------
[ '","',
  index: 32,
  input: '=HYPERLINK("https://example2.com","Example 2")' ]
null
---------------------------
null
[ ',"Example 3"',
  '"Example 3"',
  index: 33,
  input: '=HYPERLINK("https://example3.com","Example 3")' ]
---------------------------
[ '"https://example4.com"',
  index: 11,
  input: '=HYPERLINK("https://example4.com","Example 4")' ]
null
---------------------------

示例2:

const url_re = /".*?"/g;
const linkName_re = /\,(".*?")/g;

console.log(url_re.exec('=HYPERLINK("https://example.com","Example")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example2.com","Example 2")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 2")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example3.com","Example 3")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 3")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example4.com","Example 4")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 4")'));
console.log("---------------------------");

输出:

[ '"https://example.com"',
  index: 11,
  input: '=HYPERLINK("https://example.com","Example")' ]
[ ',"Example"',
  '"Example"',
  index: 32,
  input: '=HYPERLINK("https://example.com","Example")' ]
---------------------------
[ '","',
  index: 32,
  input: '=HYPERLINK("https://example2.com","Example 2")' ]
null
---------------------------
null
[ ',"Example 3"',
  '"Example 3"',
  index: 32,
  input: '=HYPERLINK("https://example.com","Example 3")' ]
---------------------------
[ '"https://example4.com"',
  index: 11,
  input: '=HYPERLINK("https://example4.com","Example 4")' ]
null
---------------------------

2 个答案:

答案 0 :(得分:1)

为什么不尝试使用正则表达式中的组来匹配URL和名称。像这样的东西

\("(.*?)".*?\"(.*?)"

完整的例子可能是这样的

var linkList = ['=HYPERLINK("https://example.com","Example")', '=HYPERLINK("https://example2.com","Example 2")', '=HYPERLINK("https://example3.com","Example 3")', '=HYPERLINK("https://example4.com","Example 4")'];
var myRegexp = /\("(.*?)".*?\"(.*?)"/g;
var match = myRegexp.exec(linkList);
while (match != null) {
  console.log(match[1])
  console.log(match[2])
  console.log("---------------------------");
  match = myRegexp.exec(linkList);
}

答案 1 :(得分:1)

删除g标记。

  

如果正则表达式使用“g”标志,则可以多次使用exec()方法在同一字符串中查找连续匹配。 MDN

const url_re = /".*?"/;
const linkName_re = /\,(".*?")/;

console.log(url_re.exec('=HYPERLINK("https://example.com","Example")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example2.com","Example 2")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 2")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example3.com","Example 3")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 3")'));
console.log("---------------------------");
console.log(url_re.exec('=HYPERLINK("https://example4.com","Example 4")'));
console.log(linkName_re.exec('=HYPERLINK("https://example.com","Example 4")'));
console.log("---------------------------");