473,509 Members | 4,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript trim nightmare

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
Sep 27 '07 #1
7 2902
On Sep 27, 1:50 pm, Henri <he...@dontevenbother.frwrote:
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.

Sep 27 '07 #2
Henri wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 27 '07 #3
Henri said the following on 9/27/2007 1:50 PM:
Hello,

any idea why a regexp-based trim function would work fine in firefox but
not in ie?
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:

<URL: http://jibbering.com/faq/index.html#FAQ4_16>
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.
I wish IE didn't exist either. Then I wouldn't have to read people's
rants about it.

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

var theString = "String with trailing spaces ";
alert('>' + theString.trim() + '<')

alerts >String with trailing spaces<

So, what is it about it that "doesn't work" in IE?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '07 #4
On Thu, 27 Sep 2007 14:50:10 -0400, Randy Webb wrote:
Henri said the following on 9/27/2007 1:50 PM:
>Hello,

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

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:

<URL: http://jibbering.com/faq/index.html#FAQ4_16>
>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.

I wish IE didn't exist either. Then I wouldn't have to read people's
rants about it.
i like to hate ie.
String.prototype.trim = function() {
re = /\s*$/g;
return this.replace(re, "");
}

var theString = "String with trailing spaces "; alert('>' +
theString.trim() + '<')

alerts >String with trailing spaces<

So, what is it about it that "doesn't work" in IE?
Sep 28 '07 #5
On Thu, 27 Sep 2007 11:00:45 -0700, David Mark wrote:
On Sep 27, 1:50 pm, Henri <he...@dontevenbother.frwrote:
>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.
thank you
Sep 28 '07 #6
Henri said the following on 9/28/2007 3:15 AM:
On Thu, 27 Sep 2007 14:50:10 -0400, Randy Webb wrote:
>Henri said the following on 9/27/2007 1:50 PM:
<snip>
>>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.
I wish IE didn't exist either. Then I wouldn't have to read people's
rants about it.
i like to hate ie.
That's nice[1]. It would be even nicer if you had some reason to hate it
other than your inability to debug bad code.
[1]<URL: http://members.aol.com/_ht_a/hikksnotathome/thatsNice.html>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 28 '07 #7
On Fri, 28 Sep 2007 03:39:13 -0400, Randy Webb wrote:
Henri said the following on 9/28/2007 3:15 AM:
>On Thu, 27 Sep 2007 14:50:10 -0400, Randy Webb wrote:
>>Henri said the following on 9/27/2007 1:50 PM:

<snip>
>>>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.
I wish IE didn't exist either. Then I wouldn't have to read people's
rants about it.
i like to hate ie.

That's nice[1]. It would be even nicer if you had some reason to hate it
other than your inability to debug bad code.
[1]<URL: http://members.aol.com/_ht_a/hikksnotathome/thatsNice.html>
funny
Sep 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.