I have come across a piece of code in JScript:
RegExp.$1
Does anybody know what it does?
If I output it on its own, I get nothing not e
The literal expression RegExp.$1
will get you the value of the first capture group of the last regex ran. Whatever that regex was.
For example:
var match = /_(.*)_/.exec('_test_');
var newMatch = '123-abc'.match(/(\d*)-(\w*)/);
var num = RegExp.$1; // '123';
RegExp.$1
is globally available, so it can be accessed from anywhere in your page, regardless of where the regex itself was ran.
I've never seen this syntax used before seeing this question, and I wouldn't suggest using it, as I cannot find documentation on it. Also, any regex ran on your page, regardless of where, will modify this property. If you want to get the capture groups, I'd use the arrays returned from String.match
or RegExp.exec
instead.
EDIT: I found some documentation about this: http://msdn.microsoft.com/en-us/library/ie/24th3sah(v=vs.94).aspx
EDIT 2: I found some more info about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties
RegExp.$1
is deprecated. That means future browsers might remove this "feature", so I suggest not using it.