0

I can't seem to find a way for a javascript regex to match on the whole text, and not line by line. I'd like that the regex engine do not regard '\n' as the end of the text, but as a character like any other.

Here's an example of the problem:

var string = "Aaa\nbb";
console.log(/^A(.*)/g.exec(string)[1]);

Result in the console:

aa
John Smith Optional
  • 22,259
  • 12
  • 43
  • 61

1 Answers1

2

. does not match line breaks and unfortunately JavaScript does not have a s switch to make it match line breaks as well. So you can’t use . or only together with [\n\r] like:

/(.|[\n\r])+/

It would be simpler to use an expression other than . like [^], which will match any character. So:

/[^]+/
Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844