jpalladino84
jpalladino84

Reputation: 2510

Does V8 automatically parse ascii characters from strings?

I noticed some inconsistencies in Chrome vs. Firefox. I was basically trying to create a new date from a string I scraped off the Dom.

// Assume dateString was pull from a Dom node.
var dateString = 'Nov\xa025, 2013'; 
var date = new Date(dateString);

You will notice that dateString contains the ascii non-breaking space character in it. So when ran in the Chrome console, date == valid date. Firefox on the other hand doesn't like the ascii character and date != valid date.

The remedy would be to just replace the ascii with an actual space. I am curious as to if V8 is doing this cleaning of string for us, not just for new Date()?

Upvotes: 1

Views: 166

Answers (2)

apsillers
apsillers

Reputation: 115920

No, both browsers parse string literals identically:

> 'Nov\xa025, 2013'.charCodeAt(3)
160

> 'Nov 25, 2013'.charCodeAt(3)
32

However, the Date constructors differ between the browsers. The EMCAScript specification requires only one date format (namely, YYYY-MM-DDTHH:mm:ss.sssZ) but custom date formats may be freely supported by an implementation:

If the String does not conform to that [ECMAScript-defined] format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

Apparently Chrome supports a custom date format that allows non-breaking spaces, where Firefox does not.

Upvotes: 2

tomdemuyt
tomdemuyt

Reputation: 4592

It leaves the string as is, you can try

dateString.charCodeAt(3)

to confirm that.

Upvotes: 1

Related Questions