正则表达式可以同时选择和忽略匹配项吗?

时间:2018-12-21 12:18:51

标签: javascript regex

我有一个正则表达式3可以检测大文本中的电话,然后出于隐私原因将其替换为“(phone)”。

var phoneNumber = /\(?(?:[0-9]{2,4})\)?[- . ]{0,}?(?:[0-9]{2,4})[- . ]{0,}?(?:[0-9]{2,4})/gim;

var separatePhone = /(\d\s){8,}\d/gim;

var textPhone = /((one|two|three|four|five|six|seven|eight|nine|zero|\d)\s){8,}(one|two|three|four|five|six|seven|eight|nine|zero|\d)/gim;

然后我检查我的文本是否与每个文本都匹配。

示例:

I need 100000€ call me at 123456789 to talk about it.

Hi, can you buy this item (link) for $500.000? call me on +01123456789

Hi, can you buy this item (link) for £100000000? call me at 100000000

本文中的问题是100000€被检测为电话。

我想匹配电话,但不匹配带有€,$或£(数字开头的$和£)的电话,我已经有这个(?![\€\$\£]\(?(?:[0-9]{2,4})),也已经尝试了:

(?![\€\$\£]\(?(?:[0-9]{2,4}))(?=\(?(?:[0-9]{2,4})\)?[- . ]{0,}?(?:[0-9]{2,4})[- . ]{0,}?(?:[0-9]{2,4}))

但这不起作用。

我在https://regex101.com/r/PJbIm9/14中检查了此正则表达式

更新

我将使用此表达式(项目中的原始表达式),将其标记为解决方案,然后将正则表达式检查为数组。

/[^$€£0-9]((?:[0-9]{2,4})\)?[- . ]{0,}?(?:[0-9]{2,4})[- . ]{0,}?(?:[0-9]{2,4})+)/gim;

2 个答案:

答案 0 :(得分:2)

通常,您可以使用以下机制:

(?: everything you do not want) | (important stuff)

然后以编程方式检查是否已设置组1,例如对于您给出的示例:

(?:\$[\d.]+|\d+€)|(\d+)

如果遇到其他货币,请更改非捕获组中的左分支,并根据需要添加替代。参见your modified demo on regex101.com


JavaScript中应该是

let data = `I need 100000€ call me at 123456789 to talk about it.

Hi, can you buy this item (link) for $500.000? call me on +01123456789`;

let rx = /(?:\$[\d.]+|\d+€)|(\d+)/g;
match = rx.exec(data);
while (match != null) {
    if (typeof(match[1]) != 'undefined')
        console.log(match[1]);
    match = rx.exec(data);
}

答案 1 :(得分:1)

为什么不这样简单?

(480,480)

请参见a demo on regex101.com