Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: SV: Re: trim() in JavaScript


Message #1 by "Robert Nyman" <robert.nyman@c...> on Thu, 31 Oct 2002 15:02:14 +0100
Adding your own prototype methods for built-in objects is so beautiful!
:-)
Very nice!

(Haven't tried the RegExp, though)


/Robert


-----Ursprungligt meddelande-----
Fr=E5n: SARGHI Mircea Ionut [mailto:syms@i...]
Skickat: den 31 oktober 2002 13:16
Till: javascript
=C4mne: [javascript] Re: trim() in JavaScript



Hi...do this in a more elegant way...
String.prototype.trim =3D function()
	{
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}

Add the code above to your script and then to trim a string just use
[string_name].trim();

Good luck.

On Thu, 31 Oct 2002, Chris Scott wrote:

> Hi Hovik,
>
> > I know a function trim() in VBScript to remove blank places in text.

> > What's the similar function in JavaScript.
>
> I'm not sure there is one, but you can use something like...
>
> function Trim(str){
>  return str.replace(/^\s*(.*\S+)?\s*$/, "$1");
> }
>
> (I have only tested this in IE6, NS4.79 & NS6).
>
> HTH,
>
> Chris
>
>
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> 
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogram
> me
> r-20
> Constructing Accessible Web Sites
>
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
> r-20
> Practical JavaScript for the Usable Web
>
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
> r-20
>



---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogramm
e
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
r-20


  Return to Index