I'm doing a JavaScript plugin, launched at every page-load, that replaces every matching structure with a link... That link redirects to a web application/database. A resource for coders of the Mount&Blade game.
In theory is easy, but I've found an huge obstacle in my way to the success: Regular expressions.
Even helped by a program named QuickRegex I can't get the structure to match. Or if I don't do a proper conditioning it outputs wrong results. The matching structure is as follows:
(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class),
I want to pick item_set_slot
and turn it into a link to http://mbcommands.ollclan.eu/#$1
This is the code I'm using, that works, more or less. ;)
/* Mount&Blade Command Database Linking by Swyter */
function swymbcommandshooker(){
/* Regular HTML Expressions */
document.getElementsByTagName("body")[0].innerHTML=document.getElementsByTagName("body")[0].innerHTML.replace(/[\(]([a-zA-Z_]+)[\,]/gi, "(<a href='http://mbcommands.ollclan.eu/#$1' title='[?] Take an look in the Command Database' target='_blank'>$1</a>,");
/* Python highlighter Support...*/
document.getElementsByTagName("body")[0].inne开发者_高级运维rHTML=document.getElementsByTagName("body")[0].innerHTML.replace(/(</span>([_a-z]+)\,/gi, "(</span><a href='http://mbcommands.ollclan.eu/#$1' title='[?] Take an look in the Command Database' target='_blank'>$1</a>,");
}
addOnloadHook( swymbcommandshooker );
Thanks in advance.
Hm, I'm not sure if I have understand you correctly, but if you really just want the match "item_set_slot" in "(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class)," the following regex should do:
/^\(([a-z_]+),/i
The JavaScript to generate the URL could look like this:
var tuple = '(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class),';
var url = tuple.replace(/^\(([a-z_]+),.*/i, 'http://mbcommands.ollclan.eu/#$1');
Note the appended .*
in the regex, which is needed to match the rest of the tuple.
精彩评论