JavaScript Regular Expressions

When you invoke any of the following methods, and a match is found, the global RegExp object is updated:

exec()
match()
test()
search()
replace()
split()

RegExp.$1
RegExp.$2
RegExp.$3
RegExp.$4
RegExp.$5
RegExp.$6
RegExp.$7
RegExp.$8
RegExp.$9

Read-only properties that contain the text matched by the corresponding set of parentheses in the last pattern matched. The number of possible parenthesized substrings is unlimited, but the RegExp object can only hold the last nine.

That sums up all the properties of the RegExp object, under Navigator 4.0x and Internet Explorer 4.0. Some of these properties might not be crystal clear yet, so let’s take a look at an example:

var str = “START http://www.webreference.com/js/index.html END”;
var ar = str.match(/(\w+)://([^/:]+)(:\d*)?([^# ]*)/);
We’ll use a few scripts to find the value of each of the properties. The first one:

document.write(RegExp.$1, ”
“,
RegExp.$2, ”
“,
RegExp.$3, ”
“,
RegExp.$4);
produces the following output (notice that one of the properties is not defined, so a blank line is created):

http <– RegExp.$1
www.webreference.com <– RegExp.$2
<– RegExp.$3
/js/index.html <– RegExp.$4


from : http://www.webreference.com/js/column5/backreferences.html

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.