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

Escaped characters in php style date (date.js) #76

Closed
Haravikk opened this issue Apr 7, 2013 · 2 comments
Closed

Escaped characters in php style date (date.js) #76

Haravikk opened this issue Apr 7, 2013 · 2 comments
Assignees

Comments

@Haravikk
Copy link

Haravikk commented Apr 7, 2013

I was looking at datetime/date.js and I'm not sure the escaping behaves quite as it should.
The relevant RegExp is: /\\?([a-z])/gi

It works fine for detecting if a letter is immediately preceded by a slash, but doesn't take into account slashes that are themselves escaped. For example a format string of "H\\\\i\\\\s", should produce a result like "17\57\31", but would in fact see both i and s as escaped even though they're not.

I've been working from the same basic code from jsfromhell.com, and reinventing the wheel a bit since somehow I missed this version of the PHP style date function. Anyway, the regexp I've been using is: /(\\\\)*(\\?([a-Z]))/gi

This will match any paired backslashes first, and only catch any remnant ones as escape sequences for a format letter. So then all you need to do is throw it into:

formatChrCb = function(m, e, t, c) { return e + (f[t] ? f[t]() : c); };

Hopefully I've not completely misunderstood!

@theriault
Copy link
Collaborator

Good catch. Thanks, Haravikk!

I dug a little deeper. It appears a backslash can escape any character (not just [a-z] and \\). If there's no character to escape (passing just \\ or trailing a string with \\), then PHP drops it from the result. Taking all of this into consideration, I believe the fix to the RegExp should be: /\\?(.?)/gi No other code would need to change.

@brettz9
Copy link
Collaborator

brettz9 commented Jul 12, 2013

I applied your fix Theriault as it appeared to work with the following test cases (not added as part of source given the already large number of examples) in addition to appearing to work normally with the regular flags:

alert(date('') === '')
alert(date('\\') === '')
alert(date('\\\\') === '\\')
alert(date('\\\\\\') === '\\')
alert(date('\\t') === 't')

Thanks, Haravikk, for bringing up a useful regex approach for other backslash use cases, and thanks to you both...

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