aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes calling a function with a backreference in String.Replace() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "calling a function with a backreference in String.Replace()" Watch "calling a function with a backreference in String.Replace()" New topic
Author

calling a function with a backreference in String.Replace()

Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
I'm tryin to run a regex replace() on something like the following:


What I want the replace to do, is to take the matched text (in this case "replaceMe"), and pass it to a javascript eval(). So what I'm basically doing is this:



Which doesn't work :-(.

Can anyone point me in the right direction to get this working?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First off, isn't the vertical pipe significant to a regex? It would need to be escaped.

Secondly, I don't believe "$1" will have any meaning outside of the regex itself (I actually thought backreferences were in the form \n, but I've only rarely used them in JS). You might have to exec() the regex to capture the values and do the eval() replacement.

I'd also consider just using any of the existing templating engines rather than rolling your own ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling a function with a backreference in String.Replace()