Chobeat
Chobeat

Reputation: 3535

Regex not working in Javascript with IE 7

last_tag="abcde   x";
last_tag = last_tag.replace(/[\s]+x$/, '');

this is my problem: i have to remove an "x" at the end of my string. This piece of code is used in a plugin i've been using without problems until now. On IE 7 "last_tag" is selected in the wrong way, so i get an "x" and i have to remove it. I think who wrote the plugin added this replace to do exactly this but it's not working on IE7.

Example: before:last_tag="abcde x" after: last_tag="abcde"

Actually the problem is that last_tag remain exactly the same.

is the regex correct? is there any error or compatibility issue with IE?

EDIT: Probably the regex is not the issue.

I've tried this piece of code, but nothing happens:

var temp_tag="abc x";
alert(temp_tag);
temp_tag = temp_tag.replace(/[\s]+x$/, '');
alert(temp_tag)

The same piece of code work perfectly on Chrome.

Upvotes: 1

Views: 1693

Answers (3)

yunzen
yunzen

Reputation: 33439

I'd go for this RegExp

/\s+x$/

don't use character class [] for \s which is a character class already
(shorthand for something like [ \t\r\n\v\f]) (space, tab, carriage return, line feed, vertical tab, form feed)


edit
Alan Moore is right: try this instead

/[\s\u00A0]+x$/

edit
maybe this is case sensitive: maybe \u00a0would not be correct

this should match every white-space-character as well as the non breaking spaces

Upvotes: 2

Alan Moore
Alan Moore

Reputation: 75222

The regex looks okay, but it's possible you're trying to match non-breaking spaces (U+00A0). \s doesn't match those in IE (as explained in this answer), but it does in FireFox and Chrome.

Upvotes: 3

narek.gevorgyan
narek.gevorgyan

Reputation: 4185

Try this

last_tag = last_tag.replace(/[\t\r\n]+x$/, '');

Upvotes: 0

Related Questions