-1

Given the following strings

I have the following regex

^\/\/\s(prevent\s?eval\s|eval\s?prevent)\s?([^\n]*)$

What I am trying to do is match the following:

// preventeval
hello world

[preventEval]

// preventeval meow
hello world

[preventEval, meow]

https://regexr.com/529p7

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

1 Answers1

1

\s matches any whitespace character, and a newline character is a whitespace character, so it gets matched by it (and then the whole next line is matched by ([^\n]*)).

Optionally match a plain space instead (and remove the \s from the end of prevent\s?eval\s in the first alternation).

You can also use .* instead of [^\n]*, you probably wouldn't want to match a linefeed character anyway:

^\/\/\s(prevent\s?eval|eval\s?prevent) ?(.*)$

It sounds likely that you don't want any of the \ss to match newlines in which case, replace them all with plain spaces:

^\/\/ (prevent ?eval|eval ?prevent) ?(.*)$
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320