RegExp Reference Properties
- turned_in_notReference Properties
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastParen ($+)
RegExp.lastMatch ($&)
RegExp.leftContext ($`)
RegExp.rightContext ($')
- To run below examples, try it inside
<script>
tag, in a basic html file
RegExp.$1-$9
To get Nth group substring match
Syntax - Use anyone
RegExp.$N // where N ranges from 1-9
RegExp["$N"] // where N ranges from 1-9
For example :
var str="bookWorld";
var pattern=/(oo)(..)/ig;
pattern.exec(str);
alert(RegExp["$1"]);
// Output: oo
Comment : Return the 1st group (oo) match ('oo' in above case)
RegExp.input ($_)
To get the string against which a current regular expression is matched
Syntax - Use anyone
RegExp.input //Full-format
RegExp.$_ //Short-format
RegExp["$_"] //Short-format
For example :
var str="bookWorld";
var pattern=/(oo)(..)/ig;
pattern.test(str);
alert(RegExp.$_);
// Output: bookWorld
Comment : Return used input text ('bookWorld' in above case)
RegExp.lastParen ($+)
To get the last group substring match
Syntax - Use anyone
RegExp.lastParen
RegExp.$+
RegExp['+'] // use this since, "+" has a special meaning in javaScript
For example :
var str="bookWorld";
var pattern=/(oo)(..)/ig;
pattern.exec(str);
alert(RegExp["$+"]);
// Output: kW
Comment : Return the last group (..) match ('kW' in above case)
RegExp.lastMatch ($&)
To get the last matched substring
Syntax - Use anyone
RegExp.lastMatch
RegExp.$&
RegExp['$&'] // use this since, "&" has a special meaning in javaScript
For example :
var str="WorkBook";
var pattern=/kB/ig;
pattern.test(str);
alert(RegExp["$&"]);
// Output: kB
Comment : Returns last matched substring ('kB' in above case)
RegExp.leftContext ($`)
To get the substring preceding the most recent match
Syntax - Use anyone
RegExp.leftContext
RegExp.$`
// use this since, "`" has a special meaning in javaScript
RegExp['$`']
For example :
var str="WorkBook";
var pattern=/kB/ig;
pattern.test(str);
alert(RegExp["$`"]);
// Output: Wor
Comment : Returns left-side of last matched substring ('Wor' in above case)
RegExp.rightContext ($')
To get the substring following the most recent match
Syntax - Use anyone
RegExp.rightContext
RegExp.$'
RegExp["$'"] // use this since, "'" has a special meaning in javaScript
For example :
var str="WorkBook";
var pattern=/kB/ig;
pattern.test(str);
alert(RegExp["$'"]);
// Output: ook
Comment : Returns right-side of last matched substring ('ook' in above case)
* Important - Useful sometimes
You can try reference properties except "RegExp.input ($_)" with javaScript string replace( ) function
- Here, you can directly use symbols of the specific reference properties
var str="bookWorld goodWorld";
var pattern=/(oo)(..)/ig;
alert(str.replace(pattern,"@-$&-@"));
// Output: b@-ookW-@orld g@-oodW-@orld
Comment : It will replace all matches made with pattern to "@-$&-@"
Where "$&" will hold,
for the 1st matched substring (which in above case $& = "ookW") and
for the 2nd matched substring (which in above case $& = "oodW")
So, finally
1st match replaced with "@-ookW-@" and
2nd match replaced with "@-oodW-@"