This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!