Trim a string with all types of spaces

August 2, 2007 · Posted in javascript 

var a = “151 “;
a = a.replace(/^\s+|\s+$/, “”);

//a is now “151″ .

Note:
\s : Matches a single white space character, including space, tab, form feed, line feed.
Equivalent to [ \f\n\r\t\v\u00A0\u2028\u2029].

\S: Matches a single character other than white space.
Equivalent to [^ \f\n\r\t\v\u00A0\u2028\u2029].

Comments

Leave a Reply