The comment about obfuscating email addresses led me to look for Javascript functions that reverse a string, or translate characters á la PHP’s strtr() function.
Of course, they don’t exist natively, so here are my versions that extent the String class:
The code for the translate() method was copied from the PHP source code. If there are more efficient ways to do it, feel free to comment!
Copyright © 2000-2012 Colin Viebrock • All Rights Reserved
Don’t miss http://blog.stevenlevithan.com/archives/faster-trim-javascript and http://phpjs.org/functions/index :)
11 February 2010, 18:40 • PermaLink
Thanks Steve. I like the analysis of the various trim implementations. I should change the regexes above to /^\s\s*/ and /\s\s*$/ which gives me the same code as trim1() … a good all-around approach.
The strtr() equivalent at phpjs.org seems like overkill, although it does handle the case where you can pass an array instead of $from and $to strings.
12 February 2010, 11:29 • PermaLink