So I have a string read from a JavaScript file, that will have:
...
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 mented.
So when I run
while( match = requireRegExp.exec(str) ){
...
}
I will only get a match for the unmented line that starts with require...
So I have a string read from a JavaScript file, that will have:
...
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 mented.
So when I run
while( match = requireRegExp.exec(str) ){
...
}
I will only get a match for the unmented line that starts with require...
Share Improve this question edited May 25, 2015 at 16:24 Alex 86514 silver badges24 bronze badges asked May 25, 2015 at 14:29 André Alçada PadezAndré Alçada Padez 11.6k26 gold badges71 silver badges128 bronze badges 2-
str = str.replace(/\/\/[^\r\n]*/g, '');
– Deadooshka Commented May 25, 2015 at 14:35 -
Not sure if this is what you need but
regequireRegExp = /^require.+/g
worked for me. – Alex Commented May 25, 2015 at 14:37
2 Answers
Reset to default 6regequireRegExp = /^\s*require\('([^']+)'\);/gm
Explanation:
^
assert position at start of a line
\s*
checks for a whitespace character 0 ... many times
require
matches the word require
\(
matches the character (
'
matches the character '
([^']+)
matches anything that isnt a '
1 ... many times
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)
m
modifier: multi-line. Causes ^
and $
to match the begin/end of each line (not only begin/end of string)
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./r/kQ0lY8/3
If you want to match only those with require
use the following:
/^\s*require.*/gm
See DEMO
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744195106a4562619.html
评论列表(0条)