javascript - External replace function not working -


why have place replace function inside str.replace statement?

this works fine:

str = str.replace(/&|<|>|"|'/g, function replacer(match) { switch (match) {   case "&":     return "&amp;";   case "<":     return "&lt;";   case ">":     return "&gt;";   case '"':     return "&quot;";   case "'":     return "&apos;"; } }); 

this not work, returning "reference error: match not defined":

str = str.replace(/&|<|>|"|'/g, replacer(match));  function replacer(match) { switch (match) {   case "&":     return "&amp;";   case "<":     return "&lt;";   case ">":     return "&gt;";   case '"':     return "&quot;";   case "'":     return "&apos;"; } } 

why not able call replacer() external function? passing arguments breeze other functions, not in context - within str.replace statement. curious, if curiosity allowed. besides, bugs me... thanks!

(searched , double searched everywhere answer before posting)

call this:

str = str.replace(/&|<|>|"|'/g, replacer); 

meaning pass function, not function call result.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -