Nodejs regexp错误:“无效正则表达式:无重复”

时间:2022-02-26 20:25:19

So I have this regular expression:

我有一个正则表达式

(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#exclude[ \t]*([^\n*]*)[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?((?:.|\n|\r)*?)(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#endexclude[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?

Which is supposed to match anything that looks like this:

它应该匹配任何像这样的东西:

/* #exclude */
hurdur = somerandomtextorcode;
/* #endexclude */

I am trying this regular expression in tools such as https://regex101.com/ (https://regex101.com/r/eA5oK9/1) and there it just simply works, no errors.

我正在工具中尝试这个正则表达式,例如https://regex101.com/ (https://regex101.com/r/eA5oK9/1),在那里它仅仅工作,没有错误。

However, in the nodejs environment, I get this error which I really do not know how to solve:

但是,在nodejs环境中,我得到了这个错误,我真的不知道如何去解决:

Warning: Invalid regular expression: /(?:[      ]*)?(?://|/*)[  ]*#exclude[
]*([^
]*)[    ]*(?:*/)?(?:[   ]*[
]+)?((?:.|
|
)?)(?:[         ]*)?(?://|/*)[  ]*#endexclude[  ]*(?:*/)?(?:[   ]*[
]+)?/: Nothing to repeat Use --force to continue.

Any help on this would be greatly appreciated!

如有任何帮助,我们将不胜感激!

2 个解决方案

#1


1  

Alright, it turns out it was a problem related to the way I was actually creating the regular expression.

好吧,原来这是一个和我创建正则表达式的方式有关的问题。

I was creating (and applying) the regular expression like this:

我正在创建(并应用)这样的正则表达式:

var rExclude = '(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#exclude[ \t]*([^\n*]*)[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?((?:.|\n|\r)*?)(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#endexclude[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?';

contents = contents.replace(new RegExp(rExclude, 'gi'), function () { return ""; });

This gives me the error that is described in the starting post. However, because the regular expression is in a string, JavaScript decides it wants to handle the regular expression differently. You can fix it in two ways:

这就给出了开始文章中描述的错误。但是,由于正则表达式位于字符串中,所以JavaScript决定要以不同的方式处理正则表达式。你可以用两种方法修复它:

Solution 1 This solution alters the way stuff is being escaped in your string.

解1这个解改变了字符串中转义的方式。

var rExclude = '(?:[ \t]*)?(?://|/\\*)[ \t]*#exclude[ \t]*(?:\\*/)?(?:.|\n|\r)*?(?://|/\\*)[ \t]*#endexclude[ \t]*(?:\\*/)?';

contents = contents.replace(new RegExp(rExclude, 'gi'), function () { return ""; });

Solution 2 This solution alters the way the actual regular expression is being created:

解决方案2这个解决方案改变了创建实际正则表达式的方式:

contents = contents.replace(/(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#exclude[ \t]*(?:\*\/)?(?:.|\n|\r)*?(?:\/\/|\/\*)[ \t]*#endexclude[ \t]*(?:\*\/)?/gi, function () { return ""; });

It's just another weird ass JavaScript quirk, unfortunately.

不幸的是,这只是另一个怪异的JavaScript怪癖。

#2


-1  

Double-up your backslashes.

合力完成你的反斜杠。

Notice how here: (?:*/)? there's no backslash? This means your * is trying to repeat.. nothing. Nothing to repeat.

注意:(?:* /)?没有反斜杠吗?这意味着你的*试图重复…什么都没有。没有重复。

#1


1  

Alright, it turns out it was a problem related to the way I was actually creating the regular expression.

好吧,原来这是一个和我创建正则表达式的方式有关的问题。

I was creating (and applying) the regular expression like this:

我正在创建(并应用)这样的正则表达式:

var rExclude = '(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#exclude[ \t]*([^\n*]*)[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?((?:.|\n|\r)*?)(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#endexclude[ \t]*(?:\*\/)?(?:[ \t]*[\r\n]+)?';

contents = contents.replace(new RegExp(rExclude, 'gi'), function () { return ""; });

This gives me the error that is described in the starting post. However, because the regular expression is in a string, JavaScript decides it wants to handle the regular expression differently. You can fix it in two ways:

这就给出了开始文章中描述的错误。但是,由于正则表达式位于字符串中,所以JavaScript决定要以不同的方式处理正则表达式。你可以用两种方法修复它:

Solution 1 This solution alters the way stuff is being escaped in your string.

解1这个解改变了字符串中转义的方式。

var rExclude = '(?:[ \t]*)?(?://|/\\*)[ \t]*#exclude[ \t]*(?:\\*/)?(?:.|\n|\r)*?(?://|/\\*)[ \t]*#endexclude[ \t]*(?:\\*/)?';

contents = contents.replace(new RegExp(rExclude, 'gi'), function () { return ""; });

Solution 2 This solution alters the way the actual regular expression is being created:

解决方案2这个解决方案改变了创建实际正则表达式的方式:

contents = contents.replace(/(?:[ \t]*)?(?:\/\/|\/\*)[ \t]*#exclude[ \t]*(?:\*\/)?(?:.|\n|\r)*?(?:\/\/|\/\*)[ \t]*#endexclude[ \t]*(?:\*\/)?/gi, function () { return ""; });

It's just another weird ass JavaScript quirk, unfortunately.

不幸的是,这只是另一个怪异的JavaScript怪癖。

#2


-1  

Double-up your backslashes.

合力完成你的反斜杠。

Notice how here: (?:*/)? there's no backslash? This means your * is trying to repeat.. nothing. Nothing to repeat.

注意:(?:* /)?没有反斜杠吗?这意味着你的*试图重复…什么都没有。没有重复。