Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?

Reply
Thread Tools

FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?

 
 
FAQ server
Guest
Posts: n/a
 
      02-22-2007
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 4+) :

String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim=
new Function("return this.replace(/^\\s+|\\s+$/g,'')")

or for all versions (trims characters ASCII<32 not true
"whitespace"):

function LTrim(str) {
for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
return str.substring(k,str.length);
}
function RTrim(str) {
for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
return str.substring(0,j+1);
}
function Trim(str) {
return LTrim(RTrim(str));
}

http://msdn.microsoft.com/library/de...63906a7353.asp

http://docs.sun.com/source/816-6408-10/regexp.htm


===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

 
Reply With Quote
 
 
 
 
Andrew Poulos
Guest
Posts: n/a
 
      02-22-2007
FAQ server wrote:
> -----------------------------------------------------------------------
> FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
> -----------------------------------------------------------------------
>
> Using Regular Expressions (JavaScript 1.2/JScript 4+) :
>
> String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
> String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
> String.prototype.Trim=
> new Function("return this.replace(/^\\s+|\\s+$/g,'')")
>


What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.

I don't have it at hand but someone here kindly wrote one for me.

Andrew Poulos
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      02-22-2007
Andrew Poulos wrote on 22 feb 2007 in comp.lang.javascript:

> FAQ server wrote:
>> -----------------------------------------------------------------------
>> FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
>> -----------------------------------------------------------------------
>>
>> Using Regular Expressions (JavaScript 1.2/JScript 4+) :
>>
>> String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
>> String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
>> String.prototype.Trim=
>> new Function("return this.replace(/^\\s+|\\s+$/g,'')")
>>

>
> What would also be useful is another prototype that reduces multiple
> contiguous spaces to a single space.


String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Une Bévue
Guest
Posts: n/a
 
      02-22-2007
Evertjan. <(E-Mail Removed)> wrote:

> String.prototype.whiteSpaces2singleSpace =
> new Function("return this.replace(/\\s\\s+/g,' ')")



writing :

....(/\s\s+/g,' ')...

isn't sufficient no need to escape \ ???

ami i wrong ?
--
Une Bévue
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-22-2007
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:

> Evertjan. <(E-Mail Removed)> wrote:
>
>> String.prototype.whiteSpaces2singleSpace =
>> new Function("return this.replace(/\\s\\s+/g,' ')")

>
>
> writing :
>
> ...(/\s\s+/g,' ')...
>
> isn't sufficient no need to escape \ ???
>
> ami i wrong ?


Did you try?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Une Bévue
Guest
Posts: n/a
 
      02-22-2007
Evertjan. <(E-Mail Removed)> wrote:

> Did you try?


YES here it is :

<http://www.yvon-thoraval.com/JS/test_regexp.html>

and i'm TRUE
--
Une Bévue
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-22-2007
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:

> Evertjan. <(E-Mail Removed)> wrote:
>
>> Did you try?

>
> YES here it is :
>
> <http://www.yvon-thoraval.com/JS/test_regexp.html>


Nonsense, you did not try,
but did quite someting else!

You did this:

var s = p.replace(/\s\s+/g, ' ');

While my line was:

String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")

Please try that with /\\s\\s+/g replaced by /\s\s+/g

.... and you will see what goes wrong.

Do you know what goes wrong?

> and i'm TRUE


Are you Boolean?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Une Bévue
Guest
Posts: n/a
 
      02-22-2007
Evertjan. <(E-Mail Removed)> wrote:

> Please try that with /\\s\\s+/g replaced by /\s\s+/g


i have added the /\\s\\s+/g for t right now

> Do you know what goes wrong?


Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototype.whiteSpace2singleSpace implies ???


--
Une Bévue
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      02-22-2007
In article <1htxzrq.n4rk9066racbN%(E-Mail Removed) nvalid>,
http://www.velocityreviews.com/forums/(E-Mail Removed)d (Une Bévue) wrote:

> Evertjan. <(E-Mail Removed)> wrote:
>
> > Please try that with /\\s\\s+/g replaced by /\s\s+/g

>
> i have added the /\\s\\s+/g for t right now
>
> > Do you know what goes wrong?

>
> Not at all because i thought the purpose of the Regexp was to replace
>
> multiple white spaces by single one, as the name :
>
> String.prototype.whiteSpace2singleSpace implies ???


Safari doesn't like your toolkit.js as there is an extra , on line 348
or so. FF didn't seem to mind.

-- tim
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-22-2007
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:

> Evertjan. <(E-Mail Removed)> wrote:
>
>> Please try that with /\\s\\s+/g replaced by /\s\s+/g

>
> i have added the /\\s\\s+/g for t right now
>
>> Do you know what goes wrong?

>
> Not at all because i thought the purpose of the Regexp was to replace
>
> multiple white spaces by single one, as the name :
>
> String.prototype.whiteSpace2singleSpace implies ???


You are on the wrong track.

>> String.prototype.whiteSpaces2singleSpace =
>> new Function("return this.replace(/\\s\\s+/g,' ')");


new Function() here has a string as parameter,
and in javascript strings backslashes are used as escape characters.
If you need the backslashes, you will have to escape them themselves.
This is ONLY required in a string,
not in a regex declaration without a string.

Try this:

====================================
<script type='text/javascript'>

alert("return this.replace(/\s\s+/g,' ')");

alert("return this.replace(/\\s\\s+/g,' ')");

</script>
=====================================




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 06-23-2007 11:00 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 2 04-24-2007 01:59 AM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 6 12-25-2006 08:47 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 10-25-2006 11:00 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 08-28-2006 11:00 PM



Advertisments