Talk:Core JavaScript 1.5 Reference:Global Objects:RegExp
From MDC
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)
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)
[edit] 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)