Tuesday, 27 October 2009 15:55

I found this snippet while revisiting some old code, and thought it might help someone else. It's trim() for AS3, and it should work in Flash or Flex.

function trim( s:String ):String
{
  return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
}
Last Updated on Thursday, 29 October 2009 15:14
 
Comments (9)
1 Tuesday, 15 December 2009 12:37
Bad Boy
Thanks a lot!
2 Thursday, 11 February 2010 10:54
Ahem
return s.replace(/^\s+|\s+$/gs, '');
3 Thursday, 11 February 2010 12:58
Jeff Channell
Ahem,

Yours is short and doesn't use an extra function call, but does it also strip newlines and tabs?

Also, looking now at the PHP manual for trim, it seems I missed NUL and vertical tab: http://php.net/manual/en/function.trim.php
4 Saturday, 13 February 2010 07:43
Joselito
Thanks!!!
5 Friday, 19 March 2010 06:25
Flex Wizard
YOU ARE PURE 1000000% AWESOME! THANKS SO MUCH!
6 Wednesday, 21 April 2010 08:57
Original one didn't work for me
but the second one worked perfectly!
7 Wednesday, 05 May 2010 06:34
Joel Pittet
The second one is the one you need.

The \s matches any white-space character (a space, tab, newline, or return character).

http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000112.html
8 Tuesday, 15 June 2010 08:20
chichilatte
Thanxx! My tiny amendment (which also accepts null as an argument)...

function trim(s:String):String {
return s ? s.replace(/^\s+|\s+$/gs, '') : "";
}
9 Friday, 03 December 2010 05:37
SK Imdad
Use following code to trim new line spases and tabs
return s.replace(/^[\s|\t|\n]+|[\s|\t|\n]+$/gs, '');

Add your comment

Your name:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:

Featured Extensions

$25.00
$1.00
FREE
You Save: $1.00
$10.00
FREE
You Save: $10.00
$1.00
FREE
You Save: $1.00

The Joomla!® name is used under a limited license from Open Source Matters in the United States and other countries. Jeff Channell is not affiliated with or endorsed by Open Source Matters or the Joomla!® Project.

Santorum
Joomla Extensions