4 ms·
looking at the replace chains on s(val) reminded me of something a while back, is there a faster/better way to do this king of stuff? something like: return s(
by jfaucett 14y ago
looking at the replace chains on s(val) reminded me of something a while back, is there a faster/better way to do this king of stuff? something like:
return s(val).replace(re, function(t){
if( t == '>' ) return '>';
if( t == '<' ) return '<';
if( t == '&' ) return '&';
if( t == '"' ) return '"';
return t;
});
- jedschmidt 14y agonew Option("<div>in the browser, yes.</div>").innerHTML
- jasonmoo 14y agoOh I like this. I was messing with a similar tactic appending a textNode to an HTML element but this is much more elegant.
- jedschmidt 14y agoThe text node approach is cross-browser, whereas I don't think this works on oldIE.
- jsharpe 14y agoI just tried this and it doesn't seem to escape " to ".
- jfaucett 14y agoyes! awesome snippit :)
- petercooper 14y agoThis isn't for the whole thing, but a common pattern to clean up that inner part could be something like: return { '>': '>', '<': '<', '&': '&', '"': '"' }[t] || t All formatted nicer than that, of course ;-)
- tedunangst 14y agoYou can make it 50% faster by observing that replacing > is unnecessary, and only one of < or " needs replacing in any particular context.