Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: trim() in JavaScript


Message #1 by "Hovik Melkomian" <melvik@b...> on Thu, 31 Oct 2002 14:58:28 +0330
My reply was for the remark from somebody that his Netscape 4.x can't 
use
Regex :)
I hate N4 too ......

> -----Message d'origine-----
> De : Chris Scott [mailto:chris@e...]
> Envoy=C3=A9 : jeudi 31 octobre 2002 18:29
> =C3=80 : javascript
> Objet : [javascript] Re: trim() in JavaScript
>
>
> > How about doing it like in Stone Age :) ?
>
> > String.prototype.trim =3D function() {
> > var i =3D 0
> > var j =3D this.length - 1
>
> > while (i <=3D j && this.charAt(i) =3D=3D " ")
> > i++;
> > if (i > j)
> > return ""
> > else {
> > while (this.charAt(j) =3D=3D " ")
> > j--;
> > return this.substring(i, j+1)
> > }
> > }
>
> String.prototype.trim =3D function()
> {
> return this.replace(/^\s*(.*\S+)?\s*$/, "$1");
> }
>
> Will Work for NS4.79, 6 & IE6.
>
> NS4 seems to handle some regular expressions strangely (as
> well as css etc.
> etc., how much do I hate NS4?!!!)
>
>
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/th
eprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogram
me
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogram
me
r-20

  Return to Index