Revision 105798 of RegExp

  • Revision slug: Talk:JavaScript/Reference/Global_Objects/RegExp
  • Revision title: RegExp
  • Revision id: 105798
  • Created:
  • Creator: AdmiralNovia
  • Is current revision? No
  • Comment responding

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)

Yeah, how strange that they included the 'm' flag but neglected the 's' flag. In any case, this is a little more intuative:

/One(.|\n)+/m

this might better handle various line-ending characters:

/One(.|\s)+/m

AdmiralNovia 17:47, 20 March 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>
<p>Yeah, how strange that they included the 'm' flag but neglected the 's' flag. In any case, this is a little more intuative:
</p>
<pre class="eval">/One(.|\n)+/m
</pre>
<p>this might better handle various line-ending characters:
</p>
<pre class="eval">/One(.|\s)+/m
</pre>
<p><a href="User:AdmiralNovia">AdmiralNovia</a> 17:47, 20 March 2006 (PST)
</p>
Revert to this revision