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:
this might better handle various line-ending characters:
AdmiralNovia 17:47, 20 March 2006 (PST)<hr>
Removed the following totally ridiculous example:
(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 returns the total match, this should have been m
(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
- 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
lastIndexproperty.' 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 patternto 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
lastIndexproperty 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).