简体   繁体   中英

RegEx JavaScript: Match consecutive single line comments

I am trying to write a RegEx to parse groups of single line comments.

Given this input:

//line 1
//line 2
//line 3

//line 4
//line 5
//line 6

I expect to have two matches: Lines 1-3 and 4-6. With my current RegEx (^\\/\\/[\\S\\s]+$) I have one match: Lines 1-6, although there is an empty line in between.

The problem is that \\s matches any whitespace character, so the blank line is included. But at the same time, line break are part of the RegEx – but only when the line starts with // , so I am stuck.

How can I prevent the RegEx from matching the blank line?

You could try this one:

/(^\/\/[^\n]+$\n)+/gm

see here https://regex101.com/r/CrR9WU/1

This selects first the two / at the beginning of each line then anything that is not a newline and finally (at the end of the line) the newline character itself. There are two matches: rows 1 to 3 and rows 4 to 6. If you also allow "empty comment lines like // then this will do too:

/(^\/\/[^\n]*$\n)+/gm

Edit:
I know, it is a little late now, but Casimir's helpful comment got me on to this modified solution:

/(?:^\/\/.*\n?)+/gm

It solves the problem of the final \\n , does not capture groups and is simpler. (And it is pretty similar to Jan's solution ;-) ...)

This is what modifiers are for:

(?:^\/{2}.+\n?)+

With MULTILINE mode, see a demo on regex101.com .


Broken apart, this says:

 (?: # a non-capturing group ^ # start of the line \\/{2} # // .+ # anything else in that line \\n? # a newline, eventually but greedy )+ # repeat the group 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM