javascript trim nightmare

H

Henri

Hello,

any idea why a regexp-based trim function would work fine in firefox but
not in ie?

code is

String.prototype.trim = function() {
re = /\s*$/g;
return this.replace(re, "");
}

it trims trailing spaces. I've tried various regular expressions. The
point is: this works in firefox (where supplied strings are trimed fine)
but ie (always ie -oh i wish ie didn't even exist!) the function returns
an unchanged string.



Thanks
 
D

David Mark

Hello,

any idea why a regexp-based trim function would work fine in firefox but
not in ie?

code is

String.prototype.trim = function() {
re = /\s*$/g;
return this.replace(re, "");

}

it trims trailing spaces. I've tried various regular expressions. The
point is: this works in firefox (where supplied strings are trimed fine)
but ie (always ie -oh i wish ie didn't even exist!) the function returns
an unchanged string.

Thanks

String.prototype.trim = function() {
var re = /\s*$/g;
return this.replace(re, "");
};

var test = 'test ';
test = test.trim();
alert(test.length);

This alerts 4 in IE7. BTW, it isn't the problem, but you didn't
declare re. Also, you shouldn't call this trim as it only trims
trailing spaces.
 
T

Thomas 'PointedEars' Lahn

Henri said:
any idea why a regexp-based trim function would work fine in firefox but
not in ie?

code is

String.prototype.trim = function() {
re = /\s*$/g;
return this.replace(re, "");
}

it trims trailing spaces. I've tried various regular expressions. The
point is: this works in firefox (where supplied strings are trimed fine)
but ie (always ie -oh i wish ie didn't even exist!) the function returns
an unchanged string.

WFM in

- Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0)
- Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)

However, you should declare `re' with `var' (or remove it), `*' is
inefficient (use `+' instead), and `g' is unnecessary (your expression is
anchored).


HTH

PointedEars
 
H

Henri

Henri said the following on 9/27/2007 1:50 PM:

Because something else you are doing is causing it to not work in IE.
Why reinvent the wheel though? There is a perfect solution to trimming
in the group FAQ:



I wish IE didn't exist either. Then I wouldn't have to read people's
rants about it.
i like to hate ie.
 
H

Henri

String.prototype.trim = function() {
var re = /\s*$/g;
return this.replace(re, "");
};

var test = 'test ';
test = test.trim();
alert(test.length);

This alerts 4 in IE7. BTW, it isn't the problem, but you didn't declare
re. Also, you shouldn't call this trim as it only trims trailing
spaces.

thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top