正则表达式匹配未注释的行

时间:2021-01-05 01:14:29

So I have a string read from a JavaScript file, that will have:

所以我从JavaScript文件中读取了一个字符串,它将具有:

...
require('some/path/to/file.less');
...
// require('some/path/to/file.less');
...

I'm using this reg-ex:

我正在使用这个注册表:

requireRegExp = /require(\ +)?\((\ +)?['"](.+)?['"](\ +)?\)\;?/g

To catch all those lines. However, I need to filter out the ones that are commented.

抓住所有这些线。但是,我需要过滤掉评论的那些。

So when I run

所以,当我跑

while( match = requireRegExp.exec(str) ){
...
}

I will only get a match for the uncommented line that starts with require...

我将只获得以require开头的未注释行的匹配...

2 个解决方案

#1


5  

regequireRegExp = /^\s*require\('([^']+)'\);/gm

regequireRegExp = / ^ \ s * require \('([^'] +)'\); / gm

Explanation:

^ assert position at start of a line

^断言一行开头的位置

\s* checks for a whitespace character 0 ... many times

\ s *多次检查空白字符0 ...

require matches the word require

要求匹配单词require

\( matches the character (

\(匹配角色(

' matches the character '

'匹配角色'

([^']+)matches anything that isnt a ' 1 ... many times

([^'] +)匹配任何不是'1 ...多次

Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed

量词:+在一次和无限次之间,尽可能多次,根据需要回馈

' matches the character ' literally

'从字面上匹配'

\) matches the character ) literally

\)字面上匹配字符

; matches the character ; literally

;匹配角色;按照字面

g modifier: global. All matches (don't return on first match)

g修饰符:全局。所有比赛(首场比赛不返回)

m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

m修饰符:多行。导致^和$匹配每行的开头/结尾(不仅是字符串的开头/结尾)

EDIT

Apparently you wanted to get the path in a group so I edited my answer to better respond to your question.

显然你想要在小组中找到路径,所以我编辑了我的答案以更好地回答你的问题。

Here is the example:

这是一个例子:

https://regex101.com/r/kQ0lY8/3

#2


2  

If you want to match only those with require use the following:

如果您只想匹配那些需要使用以下内容:

/^\s*require.*/gm

See DEMO

#1


5  

regequireRegExp = /^\s*require\('([^']+)'\);/gm

regequireRegExp = / ^ \ s * require \('([^'] +)'\); / gm

Explanation:

^ assert position at start of a line

^断言一行开头的位置

\s* checks for a whitespace character 0 ... many times

\ s *多次检查空白字符0 ...

require matches the word require

要求匹配单词require

\( matches the character (

\(匹配角色(

' matches the character '

'匹配角色'

([^']+)matches anything that isnt a ' 1 ... many times

([^'] +)匹配任何不是'1 ...多次

Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed

量词:+在一次和无限次之间,尽可能多次,根据需要回馈

' matches the character ' literally

'从字面上匹配'

\) matches the character ) literally

\)字面上匹配字符

; matches the character ; literally

;匹配角色;按照字面

g modifier: global. All matches (don't return on first match)

g修饰符:全局。所有比赛(首场比赛不返回)

m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

m修饰符:多行。导致^和$匹配每行的开头/结尾(不仅是字符串的开头/结尾)

EDIT

Apparently you wanted to get the path in a group so I edited my answer to better respond to your question.

显然你想要在小组中找到路径,所以我编辑了我的答案以更好地回答你的问题。

Here is the example:

这是一个例子:

https://regex101.com/r/kQ0lY8/3

#2


2  

If you want to match only those with require use the following:

如果您只想匹配那些需要使用以下内容:

/^\s*require.*/gm

See DEMO