views:

72

answers:

2

I accidentially typed in \s instead of " ",

while(cname.charAt(cname.length-1) == "\s")

Aren't special characters resolved in all string literals?

Also, what is a proper regular expression to cut off all tabs and spaces from EOL? The my /(.*)[\s\t]/ selector just won't work! So I had to fallback to while(if.. substr).

+4  A: 

\s is only a character class for spaces, tabs and newlines in a regular expression. "\s" becomes just "s" in this case.

Removing tabs and spaces from EOL with:

cname = cname.replace(/\s+$/mg, '');
  • \s+ matches one or more spaces or tabs
  • $ matches the end of the subject
  • The m flag causes $ to match the end of a line, and not the string.
  • the g flag causes all matches to be replaced.
Lekensteyn
+2  A: 

\s is legal syntax within a regex, but it is not legal syntax within a string.

This is fine:

/\s/

This is illegal:

"\s"
Justice