Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String comparison functions (e.g. startsWith()) do no account for UTF-8 #160 non-breaking spaces as whitespace #377

Closed
aliatsis opened this issue Oct 3, 2013 · 3 comments

Comments

@aliatsis
Copy link

aliatsis commented Oct 3, 2013

I was trying to test "Anthony Liatsis".startsWith("Anthony "). Each of the string values were stored in variables. The "Anthony " was retrieved via a regex in my code against the text of a contenteditable div. The startsWith() result was continually false. I discovered that the trailing space in "Anthony " was UTF-8 #160 which is a non-breaking space and was not equal to an empty string.
Doing "Anthony Liatsis".startsWith("Anthony ".replace(/\s/, " ")) made it work.

@rwaldron
Copy link

rwaldron commented Oct 3, 2013

Just a heads up to the maintainer, your current implementation is correct (per ES6 String.prototype.startsWith spec) and this issue is not a bug.

"Anthony\u00A0Liatsis".startsWith("Anthony "); // false

"\u00A0" !== "\u0020"; // true

...is 100% correct.

U+0009  Tab                     <TAB>
U+000B  Vertical Tab            <VT>
U+000C  Form Feed               <FF>
U+0020  Space                   <SP>
U+00A0  Non-breaking Space      <NBSP>
U+FEFF  Byte Order Mark         <BOM>
Zs      Any other Unicode Sep.  <USP>

White space characters may occur between any two tokens and at the start or end of input. White space characters may occur within a StringLiteral, a RegularExpressionLiteral, a Template, or a TemplateSubstitutionTail where they are considered significant characters forming part of a literal value.

@andrewplummer
Copy link
Owner

@rwaldron is correct on this one. The definition of startsWith as well as the definition of whitespace should have been followed to the letter against the ES6 spec, which is why you're getting that result.

Note "Anthony Liatsis".startsWith("Anthony ".trim()) would perhaps be a bit simpler for you.

@andrewplummer
Copy link
Owner

Closing this one for now....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants