mozilla

Revision 105797 of RegExp

  • Revision slug: Talk:JavaScript/Reference/Global_Objects/RegExp
  • Revision title: RegExp
  • Revision id: 105797
  • Created:
  • Creator: Mozai
  • Is current revision? No
  • Comment example of matching any character including newline.

Revision Content

The following code returns unexpected results:

var samplere = new Regexp('One.+','m');
alert("One Two\nThree Four".match(samplere));

This returns 'One Two', not 'One Two\nThree Four'. This is confusing until you read the definition of the '.' metacharacter which is defined as "any character except newline." The following looks awkward (someone improve it please), but will match any character including newline

var betterre = new Regexp('One[\d\D]+','m');
Mozai 13:02, 23 February 2006 (PST)

Revision Source

<p>The following code returns unexpected results:
</p>
<pre class="eval">var samplere = new Regexp('One.+','m');
alert("One Two\nThree Four".match(samplere));
</pre>
<p>This returns 'One Two', not 'One Two\nThree Four'.  This is confusing until you read the definition of the '.' metacharacter which is defined as "any character except newline."  The following looks awkward (someone improve it please), but will match any character including newline
</p>
<pre class="eval">var betterre = new Regexp('One[\d\D]+','m');
</pre>
<dl><dd><a href="User:Mozai">Mozai</a> 13:02, 23 February 2006 (PST)
</dd></dl>
Revert to this revision