18

How to ignore newline in regexp in Javascript ?

for example:

 data = "\
    <test>11\n
    1</test>\n\
    #EXTM3U\n\
 "
 var reg = new RegExp( "\<" + "test" + "\>(.*?)\<\/" + "test" + "\>" )
 var match = data.match(reg)
 console.log(match[1])

result: undefined

Bdfy
  • 23,141
  • 55
  • 131
  • 179
  • possible duplicate of [How to use JavaScript regex over multiple lines?](http://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines) – Bdfy May 27 '13 at 11:01

5 Answers5

18

In JavaScript, there is no flag to tell to RegExp() that . should match newlines. So, you need to use a workaround e.g. [\s\S].

Your RegExp would then look like this:

var reg = new RegExp( "\<" + "test" + "\>([\s\S]*?)\<\/" + "test" + "\>" );
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
t.niese
  • 39,256
  • 9
  • 74
  • 101
13

You are missing a JS newline character \ at the end of line 2.

Also, change regexp to:

 var data = "\
    <test>11\n\
    1</test>\n\
    #EXTM3U\n\
 ";
 var reg = new RegExp(/<test>(.|\s)*<\/test>/);
 var match = data.match(reg);
 console.log(match[0]);

http://jsfiddle.net/samliew/DPc2E/

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
6

By reading this one: How to use JavaScript regex over multiple lines?

I came with that, which works:

var data = "<test>11\n1</test>\n#EXTM3U\n";
reg = /<test>([\s\S]*?)<\/test>/;
var match = data.match(reg);
console.log(match[1]);

Here is a fiddle: http://jsfiddle.net/Rpkj2/

Community
  • 1
  • 1
qur2
  • 450
  • 5
  • 9
3

Better you can use [\s\S] instead of . for multiline matching.

It is the most common JavaScript idiom for matching everything including newlines. It's easier on the eyes and much more efficient than an alternation-based approach like (.|\n).

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
0

EDIT: Got it:

I tried to use this regex in notepad++ But the problem is that it finds the whole text from beginning to end

MyRegex:

<hostname-validation>(.|\s)*<\/pathname-validation> (finds everything)
/<hostname-validation>(.|\s)*<\/pathname-validation>/ (finds nothing)
/\<hostname-validation\>([\s\S]*?)\<\/pathname-validation\>/ (finds nothing)
**<hostname-validation>([\s\S]*?)<\/pathname-validation> (my desired result)**

The text where I use in:

<hostname-validation>www.your-tag-name.com</hostname-validation>
          <pathname-validation>pathname</pathname-validation>   <response-validation nil="true"/>
          <validate-absence type="boolean">false</validate-absence> (...) <hostname-validation>www.your-tag-name.com</hostname-validation>
          <pathname-validation>pathname</pathname-validation>   <response-validation nil="false"/>
          <validate-absence type="boolean">false</validate-absence> (...) <hostname-validation>www.your-tag-name.com</hostname-validation>
          <pathname-validation>pathname</pathname-validation>   <response-validation nil="true"/>
          <validate-absence type="boolean">false</validate-absence> (...)
tester
  • 1
  • 1