Reputation: 28592
I'm reading a js file at here, on the very top of this js file you can find the following lines:
var
gsAgent=navigator.userAgent.toLowerCase(),
gsAppVer=navigator.appVersion.toLowerCase(),
gsAppName=navigator.appName.toLowerCase(),
gbIsOpera=gsAgent.indexOf("opera")>-1,
gbIsKHTML=gsAgent.indexOf("khtml")>-1
||gsAgent.indexOf("konqueror")>-1||gsAgent.indexOf("applewebkit")>-1,
gbIsSafari=gsAgent.indexOf("applewebkit")>-1,
gbIsIE=(gsAgent.indexOf("compatible")>-1&&!gbIsOpera)
||gsAgent.indexOf("msie")>-1,
gbIsTT=gbIsIE?(gsAppVer.indexOf("tencenttraveler")!=-1?1:0):0,
gbIsFF=gsAgent.indexOf("gecko")>-1&&!gbIsKHTML,
gbIsNS=!gbIsIE&&!gbIsOpera&&!gbIsKHTML&&(gsAgent.indexOf("mozilla")==0)
&&(gsAppName=="netscape"),
gbIsAgentErr=!(gbIsOpera||gbIsKHTML||gbIsSafari||gbIsIE||gbIsTT
||gbIsFF||gbIsNS),
gbIsWin=gsAgent.indexOf("windows")>-1||gsAgent.indexOf("win32")>-1,
gbIsVista=gbIsWin&&(gsAgent.indexOf("nt 6.0")>-1||gsAgent.indexOf("windows vista")>-1),
gbIsWin7=gbIsWin&&gsAgent.indexOf("nt 6.1")>-1,
gbIsMac=gsAgent.indexOf("macintosh")>-1||gsAgent.indexOf("mac os x")>-1,
gbIsLinux=gsAgent.indexOf("linux")>-1,
gbIsAir=gsAgent.indexOf("adobeair")>-1,
gnIEVer=/MSIE (\d+.\d+);/i.test(gsAgent)&&parseFloat(RegExp["$1"]),
gsFFVer=/firefox\/((\d|\.)+)/i.test(gsAgent)&&RegExp["$1"],
gsSafariVer=/version\/((\d|\.)+)/i.test(gsAgent)&&RegExp["$1"],
gsChromeVer=/chrome\/((\d|\.)+)/i.test(gsAgent)&&RegExp["$1"];
Now my question is what does it mean by RegExp["$1"], I can't find this syntax on the js documentation, but it's somewhat like Ruby's regex syntax. Can anyone explain it please? Great thanks.
PS: Thanks S.Mark, now I know I can use it like this:
var a="abc23de";
alert(/(\d+)/.test(a)&&parseInt(RegExp["$1"]));
But where can I find the documentation of this syntax? Even the famous book JavaScript the definitive guide has not mentioned that RegExp can be used like this.
Upvotes: 1
Views: 411
Reputation: 123841
Its a browser user agent detection script and
RegExp["$1"]
is Regular Expression Group 1, which would be
(\d+.\d+)
in /MSIE (\d+.\d+);/
For example
6.0
in MSIE 6.0
20091126
in Firefox/20091126
0.2.153.1
in Chrome/0.2.153.1
UPDATE:
var a="abc23de";
alert(/(\d+)/.test(a)&&parseInt(RegExp["$1"]));
Above codes mean, if there is numbers in the string, extract those and parse it to Integer
Upvotes: 3
Reputation: 53940
regexp.test() method populates the global RegExp object with what it has found, see the full list
/(\d+)(.+)/.test("123abc");
alert(RegExp["$1"]) // "123"
this way is deprecated, much better is to use exec() (and its string counterpart match())
var matches = /(\d+)(.+)/.exec("123abc");
alert(matches[1]) // "123"
Upvotes: 2
Reputation: 344585
MDC site was down, so the best I can offer is MSDN's documentation for the RegExp
object. In short $1, $2, $3, ...$9
are properties of the RegExp
object, containing the matches in order of appearance in the last executed regular expression.
Example (taken from MSDN):
function matchDemo(){
var s;
var re = new RegExp("d(b+)(d)","ig");
var str = "cdbBdbsbdbdz";
var arr = re.exec(str);
s = "$1 contains: " + RegExp.$1 + "\n";
s += "$2 contains: " + RegExp.$2 + "\n";
s += "$3 contains: " + RegExp.$3;
return(s);
}
The use of square braces in your example is just another way of accessing properties on an object - RegExp.$1
is functionally the same as RegExp["$1"]
(in this case). This allows you to retrieve matching groups without using the match
method.
Upvotes: 2