RegExp

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)

<hr>

Removed the following totally ridiculous example:

Example: Using <code>input</code>

In the following example, <code>RegExp.input</code>(1) is set by the Change event. In the <code>getInfo</code> function, the <code>exec</code> method uses the value of <code>RegExp.input</code> as its argument.

 <script LANGUAGE="JavaScript1.2">
 function getInfo() { // (2)
    var re = /(\w+)\s(\d+)/;
    var m = re.exec(); // (3)
    window.alert(m[0] + ", your age is " + m[2]); // (4)
 }
 </script>
 
 Enter your first name and your age, and then press Enter. (5)
 
 <form>
 <input type="text" name="NameAge" onchange="getInfo(this);"/> (6)
 </form>

(1) No RegExp.input attribute exists.

(2) No function parameter, even though the function is called with a parameter.

(3) Exec requires a parameter.

(4) m[0] returns the total match, this should have been m[1]

(5) On pressing Enter the form is submitted, not every browser fires onChange in that case.

(6) The input element is passed as object, while we only need the value property of that object.

After fixing all errors, the example does nothing more than showing simple straightforward regular expression matching in a complicated way. This example caused more confusion than it solved!

Pbb 07:29, 19 October 2006 (PDT)

 

Unicode support in Regular Expressions

I tried to clarify how Regular Expressions deal with the (Basic Multilingual Plane) Unicode characters. Hope people find this useful. If you want to check for yourselves, I wrote a Javascript RegExp Unicode Character Class tester. --Hamstersoup 03:58, 14 February 2007 (PST)

Cool, thank you for this. --Nickolay 08:09, 18 February 2007 (PST)
Misleading info about the sticky flag?
It seems that the following line is misleading: 'This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property.' This quote creates an impression that the /y flag modifies the meaning of the ^ meta char, so the two should be used together. However, the ^ should NOT be used with the sticky flag in order for the .lastIndex pattern to work. Using them together will hardly do any good at all. The meaning of the ^ is not changed by the sticky flag. Instead, the flag itself implies that the search is performed from the logical start of 'search area', specified by .lastIndex. The examples for the sticky flag in this article are correct, and they do not use the ^. The other sentence in the description of the sticky flag is also correct: 'matches only from the index indicated by the lastIndex property of this regular expression in the target string', from which it logically (and correctly) follows that the ^ is not required (and in fact, using it will ruin the whole idea because the /y flag 'will try to search' from last index, whereas the ^ 'will try to search' from the beginning of the string, notwithstanding; the two are thus mutually exclusive in most circumstances).

Document Tags and Contributors

Contributors to this page: AdmiralNovia, Pbb, Nickolay, Hamstersoup, Mozai, avatar.ds
Last updated by: avatar.ds,