javascript - Make a function in .replace() -


i have following code uses regex:

var mystring = "<<cool>> <<stuff>>" var regexstring = /<<([^\:]{0,})>>/gi  mystring.replace(regexstring, "$1") 

i able replace string based on text on capture group. like:

var mystring = "<<cool>> <<stuff>>" var regexstring = /<<([^>]{1,})>>/gi  mystring.replace(regexstring, function(var0) { //var0 being text capture group      if(var0 == "cool") {         console.log("got cool")      } else {         console.log("didn't cool")     } }) 

is there someway this?

yes, can use function second argument of .replace(). example:

mystring.replace(regexstring, function(match, group1) {      // group1 here }); 

reference: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/replace


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -