如何在JavaScript Regex中将文本与带有/不带负向前瞻的表达式匹配

时间:2021-02-05 15:18:47

Supposed to have a comma separated string of text, where each text has or not - comma separated - a token in a list like

假设有一个逗号分隔的文本字符串,其中每个文本有或没有 - 逗号分隔 - 列表中的标记,如

var tokens=['Inc.','Ltd','LLC'];

so the string is like

所以字符串就像

var companies="Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC";

I want to obtain this array as output

我想获得此数组作为输出

var companiesList = [
    "Apple Inc.",
    "Microsoft Inc.",
    "Buzzfeed",
    "Treasure LLC"
    ];

So I firstly did a RegExp like that

所以我首先做了一个这样的RegExp

var regex=new RegExp("([a-zA-Z&/? ]*),\\s+("+token+")", "gi" )

that I get the matches and search for a regex like

我得到匹配并搜索正则表达式

var regex=new RegExp("([a-zA-Z&/? ]*),\\s+("+item+")", "i" )

for each of the tokens:

对于每个令牌:

tokens.forEach((item) => {
    var regex = new RegExp("([a-zA-Z&/? ]*),\\s+(" + item + ")", "gi")
    var matches = companies.match(regex) || []
    console.log(item, regex.toString(), matches)
    matches.forEach((m) => {
        var regex = new RegExp("([a-zA-Z&/? ]*),\\s+(" + item + ")", "i")
        var match = m.match(regex)
        if (match && match.length > 2) {
            var n = match[1].trim();
            var c = match[2].trim();
            companiesList.push(n + ' ' + c);
        }
    });
});

In this way I can capture the tokens and concat matching groups 1 and 2.

通过这种方式,我可以捕获令牌和concat匹配组1和2。

var tokens = ['inc.', 'ltd', 'llc'],
  companies = "Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC",
  companiesList = [];
tokens.forEach((item) => {
  var regex = new RegExp("([a-zA-Z&/? ]*),\\s+(" + item + ")", "gi")
  var matches = companies.match(regex) || []
  console.log( item, regex.toString(), matches )
  matches.forEach((m) => {
    var regex = new RegExp("([a-zA-Z&/? ]*),\\s+(" + item + ")", "i")
    var match = m.match(regex)
    if (match && match.length > 2) {
      var n = match[1].trim();
      var c = match[2].trim();
      companiesList.push(n + ' ' + c);
    }
  });
});

console.log(companiesList)

The problem is that I'm missing the comma separated text without a token after the comma like: Buzzfeed.

问题是我在逗号之后缺少逗号分隔文本而没有令牌:Buzzfeed。

The idea is to use a non capturing group in a negative look ahead ( see here about non capturing groups in regex match)

我们的想法是使用非捕获组进行负向预测(请参阅此处有关正则表达式匹配中的非捕获组)

/([a-zA-Z]*)^(?:(?!ltd).)+$/gi 

But in this way I have any match when in the input string the token is present:

但是通过这种方式,我在输入字符串中存在令牌时存在任何匹配:

"Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure LLC".match( /([a-zA-Z]*)^(?:(?!llc).)+$/gi )

while I want to match only the text that do not have it so I would like to get - like the opposite before:

虽然我想只匹配没有它的文本所以我想得到 - 就像之前相反:

["Buzzfeed"]

So how to negate/modify the previous code to work in both cases to obtain at the end the composed array:

那么如何否定/修改以前的代码在两种情况下都可以在最后获得组合数组:

var companiesList = [
        "Apple Inc.",
        "Microsoft Inc.",
        "Buzzfeed",
        "Treasure LLC"
        ];

2 个解决方案

#1


1  

Wouldn't it be a lot easier to just reduce it, and just check the token list as you go

减少它并不容易,只需要随时检查令牌列表

var tokens    = ['Inc.','Ltd','LLC'];
var companies = "Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC";

var result    = companies.split(',').reduce( (a,b,i) => {
    return tokens.indexOf(b.trim()) === -1  ? a.push(b.trim()) : a[a.length-1] += b,a;
}, []);

console.log(result);

#2


1  

You could use a regex for splitting.

您可以使用正则表达式进行拆分。

var companies = "Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC";

console.log(companies.split(/,\s(?!Inc\.|Ltd|LLC)/i).map(s => s.replace(', ', ' ')));

#1


1  

Wouldn't it be a lot easier to just reduce it, and just check the token list as you go

减少它并不容易,只需要随时检查令牌列表

var tokens    = ['Inc.','Ltd','LLC'];
var companies = "Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC";

var result    = companies.split(',').reduce( (a,b,i) => {
    return tokens.indexOf(b.trim()) === -1  ? a.push(b.trim()) : a[a.length-1] += b,a;
}, []);

console.log(result);

#2


1  

You could use a regex for splitting.

您可以使用正则表达式进行拆分。

var companies = "Apple, Inc., Microsoft, Inc., Buzzfeed, Treasure, LLC";

console.log(companies.split(/,\s(?!Inc\.|Ltd|LLC)/i).map(s => s.replace(', ', ' ')));