Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regex replace error

9 views
Skip to first unread message

jay

unread,
Jun 11, 2008, 2:26:30 PM6/11/08
to
The following two lines work in firefox but will not evaluate in Rhino
(1.7 release 1 2008 03 06)

var str="This is the A string and the B string";
var y=str.replace(/&#(\d+);/g,function() {return
String.fromCharCode(RegExp.$1);});

y===>This is the A string and the B string

Rhino indicates that the RegExp.$1 variable is undefined and the
String.fromCharCode -- returns char(0) to be placed into the output
string.

Anyone know why this would be the case?

jay

unread,
Jun 11, 2008, 3:11:35 PM6/11/08
to

Additional note:

var y=str.replace(/&#(\d+);/g,function(z) {z.search(/(\d+)/); return
String.fromCharCode(RegExp.$1);});

above line will work if the search method is called to force setting
of the RegExp.$1 variable.

Norris Boyd

unread,
Jun 11, 2008, 3:59:52 PM6/11/08
to

All those RegExp.XX variables are non-ECMA; the following conformant
code does what you want:

var y=str.replace(/&#(\d+);/g,function(a,b) { return
String.fromCharCode(b); });

Running your sample code on a recent SpiderMonkey build gets undefined
for RegExp.$1 as well.

--N

0 new messages