function trim(str,options){
var string = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length,
j = -1;
if(options==="begin"){while(ws.test(string.charAt(++j)));return string.slice(j,i);}
if(options==="end"){while(ws.test(string.charAt(--i)));return string.slice(j+1,i+1);}
while(ws.test(string.charAt(--i)));while(ws.test(string.charAt(++j)));
return string.slice(j, i + 1);
}
This function is an implementation of the Trim() method that you find in C#.
I've added options for removing withspace both at the beginning and end. The problem is, I can't get it to work in a demo.
What I've done is this:
var a = " zareaerar arzare ";
var b = "aezze azeze a ";
var c = "azrazza rzrzrzrp"
var d = " aezzaeazeazeaz azez ";
document.write('<p style="backround:#ff0000">',trim(a,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(b,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(c,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(d,"begin"),'</p>','<br />');
First of all, no background-color appears and the strings also seem to loose the spaces in the middle of the string...
Does this work with user-input only? Are strings automatically trimmed these days?