Recently had to come up with a piece of javascript to strip off a set of illegal characters from strings before passing down to the persistance layer.
Took me a while to come up with a regex for the replace, not because it's particularly difficult, but because I suck at regexes (and I am no js expert either).
I thought it could be handy to have this functionality as a string prototype:
// strips off illegal chars &%$
String.prototype.stripOffIllegalChars = function() {
return this.replace(/[&%$]/g, "");
}
It can be used like this on any string:
var dirtyString = "blah$blah%blah&";
var cleanString = dirtyString.stripOffIllegalChars();
No comments:
Post a Comment