jeudi 12 mars 2015

regex replace all matches based on pattern


var testString = 'Please use "command1" to accept and "command2" to ignore';


All I'm trying to achive is replacing the string between the quotes but when replacing, I need to know what was inside the quotes.


After replacing it should be like:



var result = 'Please use <a href="someurl?cmd=command1">command1</a> to accept and <a href="someurl?cmd=command2">command2</a> to ignore';


I tried somethiing like this with no success:



var rePattern = /\"(.*?)\"/gi;
Text.replace(rePattern, function (match, txt, urlId) {
return "rendered link for " + match;
});

Aucun commentaire:

Enregistrer un commentaire