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 "&"; case "<": return "<"; case ">": return ">"; case '"': return """; case "'": return "'"; } });
this not work, returning "reference error: match not defined":
str = str.replace(/&|<|>|"|'/g, replacer(match)); function replacer(match) { switch (match) { case "&": return "&"; case "<": return "<"; case ">": return ">"; case '"': return """; case "'": return "'"; } }
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
Post a Comment