To complicate the matter, Javascript doesn't have a trim() function, so I built one. On the grand scale, it's not that amazing of an accomplishment, plenty of other folks already have done this, but I wanted to dabble with RegEx and develop something in a vaccume that I could compare to get an idea for how my own problem solving stacks up against the rest.
Here's what I came up with:
function trim(x)To make this work, just add it somewhere in your code and when you want to use it, just call =)
{
x = x.replace(/(^\t)(\t*)/,"").replace(/(\t*)(\t$)/,""); // tabs
x = x.replace(/(^\n)(\n*)/,"").replace(/(\n*)(\n$)/,""); // new lines
x = x.replace(/(^\r)(\r*)/,"").replace(/(\r*)(\r$)/,""); // return characters
x = x.replace(/(^<br>)(<br>*)/,"").replace(/(<br>*)(<br>$)/,""); // html returns
x = x.replace(/(^ )( *)/,"").replace(/( *)( $)/,""); // spaces
x = x.replace(/(^ )( *)/,"").replace(/( *)( $)/,""); // html spaces
return x;
}
...
strTrimmed = trim(strNotTrimmed);
...
document.write(trim(strWhatever));
...
if (strBlargh == trim(strHonk))
...


0 comments:
Post a Comment