FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? - Javascript
This is a discussion on FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? - Javascript ; -----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------
Using Regular Expressions (JavaScript 1.2/JScript 4+) :
String.prototype.lTrim =
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.rTrim =
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim =
function()
{
return this.replace(/^\s+|\s+$/g,'');
}
...
-
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------
Using Regular Expressions (JavaScript 1.2/JScript 4+) :
String.prototype.lTrim =
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.rTrim =
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim =
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. The sendings of these
daily posts are proficiently hosted by www.pair.com.
-
Re: FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
FAQ server wrote:
> -----------------------------------------------------------------------
> FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
> -----------------------------------------------------------------------
>
> Using Regular Expressions (JavaScript 1.2/JScript 4+) :
>
> String.prototype.lTrim =
> function()
> {
> return this.replace(/^\s+/,'');
> }
> String.prototype.rTrim =
> function()
> {
> return this.replace(/\s+$/,'');
> }
> String.prototype.trim =
> function()
> {
> return this.replace(/^\s+|\s+$/g,'');
> }
I have tested the following requirements to be available as of JScript
_3.1.3510_ (in standalone IE 4.01/WinNT [1]):
- Anonymous function expression
- String.prototype.replace()
- RegExp literal with `g' modifier, and without `i' modifier
- Replacing substrings matching against the aforementioned RegExp object
I have retrieved the JScript version information through ScriptEngine(),
ScriptEngineMajorVersion(), ScriptEngineMinorVersion(), and
ScriptEngineBuildVersion(), and tested several newer features (like
try...catch) to be *not* supported by that script engine. Therefore,
I am confident I got valid results.
I have updated http://PointedEars.de/scripts/es-matrix accordingly.
The FAQ should be updated as well.
PointedEars
___________
[1] http://tredosoft.com/Multiple_IE
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
-
Re: FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
Thomas 'PointedEars' Lahn said the following on 8/23/2007 8:40 PM:
> FAQ server wrote:
>> -----------------------------------------------------------------------
>> FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
>> -----------------------------------------------------------------------
>>
>> Using Regular Expressions (JavaScript 1.2/JScript 4+) :
>>
>> String.prototype.lTrim =
>> function()
>> {
>> return this.replace(/^\s+/,'');
>> }
>> String.prototype.rTrim =
>> function()
>> {
>> return this.replace(/\s+$/,'');
>> }
>> String.prototype.trim =
>> function()
>> {
>> return this.replace(/^\s+|\s+$/g,'');
>> }
>
> I have tested the following requirements to be available as of JScript
> _3.1.3510_ (in standalone IE 4.01/WinNT [1]):
<snip>
> The FAQ should be updated as well.
I have changed it locally to read JScript 3 although I don't think it
makes much difference.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Similar Threads
-
By Application Development in forum Javascript
Replies: 12
Last Post: 11-06-2007, 03:46 PM
-
By Application Development in forum Javascript
Replies: 0
Last Post: 06-23-2007, 06:00 PM
-
By Application Development in forum Javascript
Replies: 3
Last Post: 04-23-2007, 10:50 PM
-
By Application Development in forum Adobe illustrator
Replies: 2
Last Post: 02-08-2007, 10:25 AM
-
By Application Development in forum Javascript
Replies: 1
Last Post: 09-07-2004, 10:41 AM