正则expression式有时会返回结果,有时在类似的string上返回null
我有以下示例代码,其中有一个Excel公式的数组,我正在使用正则expression式试图从string中分离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 ---------------------------
你为什么不尝试在正则expression式中使用组来匹配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); }
删除g
标志。
如果您的正则expression式使用“g”标志,则可以多次使用exec()方法在同一个string中查找连续的匹配项。 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("---------------------------");