1
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.

6
  • The regex is correct and i tested it in IE7 - it's working; Did you test the same replace on other browsers? And are the two lines you've provided an exact copy of the code? Commented Nov 17, 2011 at 13:59
  • no but actually i print an alert just before and just after the "replace" and nothing happens, probably the regex is not the issue. Commented Nov 17, 2011 at 14:10
  • Yes, that's why i was asking you, if everything else is ok, the only reason this regex won't be working is - different encodings of the "x" letter in the regex and the "x" letter in your subject string. But i suppose you just have a js error somewhere before that Commented Nov 17, 2011 at 14:12
  • But just before and just after that, everything works as it does on Chrome. Commented Nov 17, 2011 at 14:19
  • So this last piece of code - what happens in IE7 - both alerts give you the same value or there are no alerts? Commented Nov 17, 2011 at 14:25

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@AlanMoore then FF and Chrome do it wrong regarding the standards and IE does it right?
@HerrSerker: No, it looks like IE is doing it wrong. The ECMAScript standard (section 15.10.2.12) calls for \s to include everything in the Unicode Zs ('Separator, Space') category, which includes U+00A0.
2

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

6 Comments

I'd go for that regex too, but I wouldn't expect it to solve the problem. \s means the same thing inside a character class as it does outside.
Perhaps to be even more error safe [\s\xA0\u00A0\u2028\u2029]
...or [\s\u00A0\u1680\u180E\u2000-\u20A0\u2028\u2029\u202F\u205F\u3000\uFEFF], which is equivalent to [\t\n\v\f\r\p{Z}\uFEFF] in flavors that support Unicode property escapes. Yes, that last one is the BOM character; don't ask me why ECMAScript tossed that one in.
@AlanMoore I thought that \uFEFF is the ZERO WIDTH NON BREAKING SPACE (fileformat.info/info/unicode/char/feff/index.htm)
@AlanMoore I see. I did a little research. What first was the replacement for U+FEFF (ZWNBSP not BOM) was U+2060 (WORD JOINER), which is deprecated now and itself somewhat replaced by U+200D (ZERO WIDTH JOINER), which I thought before is only for arabic letters (as well as ZWNJ U+200C). see here blogs.msdn.com/b/michkap/archive/2005/01/20/357028.aspx (And now the discussion is beginning to get academic :)
|
0

Try this

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

1 Comment

\s includes all \t, \r and \n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.