What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [?] "Dynamic" RegExp replace...

[?] "Dynamic" RegExp replace...
Author: Message:
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
O.P. [?] "Dynamic" RegExp replace...
Not sure on the best way to go about this...

Here's an example of what I want to do.
JScript code:
// a "dynamic" object - identifiers will vary by function
var some_variables =
{
    foo: "some text",
    bar: "some more text"
};
 
// user input - can contain such variables when surrounded with "%"
var input = "I am writing %var:foo% here.";
var regex = /%var:([a-z]+?)%/gi;
var output = input.replace(regex, some_variables[regex.$1]);

...but I just get undefined from the RegExp.  Is there a way of doing this?
08-16-2011 05:57 PM
Profile E-Mail PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: [?] "Dynamic" RegExp replace...
Don't think you can do it in one replace call, you can always just loop through them:
Javascript code:
for(var key in some_variables)
    input = input.replace(new RegExp("%var:"+key+"%", "gi"), some_variables[key]);

08-16-2011 06:19 PM
Profile PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
O.P. RE: [?] "Dynamic" RegExp replace...
Hmm, didn't think of that.  Works a treat, thanks!
08-16-2011 06:54 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: [?] "Dynamic" RegExp replace...
The reason why your version won't work is because regexp.$1 is evaluated once when that line is run.

However, you can still do this without a for-loop and lots of replace calls. JScript allows you to pass a function as second parameter for replace, whose return value will be used as replacement for each match. When using regular expressions, it'll even give you all the good stuff through the function's arguments!
Javascript code:
var output = input.replace(regex, function(match, $1) {
    // You could do literally anything here!
    return ($1 in some_variables) ? some_variables[$1] : '';
});

Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-17-2011 02:22 PM
Profile E-Mail PM Web Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
O.P. RE: [?] "Dynamic" RegExp replace...
Looks like I could do some more advanced stuff with that...  might be easier to implement stuff later on.  Thanks for that!  :P
08-17-2011 06:20 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On