I'm trying to make a router in Nodejs. A big part of that is URI -> action
, so I'll need an easy configurable list of URI's and regexping them to regexps to match against the request URI.
Simple! I've done this in PHP a million times. Give or take.
So this is what I do (to test):
> uri = '/users/#/status/*'
> uri.replace(/\//g, '\\/').replace(/#/g, '(\d+)').replace(/\*/g, '([^/]+)')
What I'm doing is 1) escaping /
and 2) replacing #
with a \d+
and 3) replacing *
with a [^/]+
In Chrome this works as expected:
< \/users\/(d+)\/status\/([^/]+)
Escaped /
and replaced #
and *
correctly. This is V8.
In Nodejs:
< \\/users\\/(d+)\\/status\\/([^/]+)
Que!? Every /
is doubly escaped? I either get a doubly escaped /
or a not escaped /
.
The regex is right, right?
I'm using Chrome 15 dev (V8 javascript) and Node 0.5.8 dev (V8 javascript). What's going on here?
Potentially interesting
If I test /^\/users\/(\d+)\/status\/([^/]+)/.test('/users/1/status/x')
it returns true in both Chrome and Node.
I'm trying to make a router in Nodejs. A big part of that is URI -> action
, so I'll need an easy configurable list of URI's and regexping them to regexps to match against the request URI.
Simple! I've done this in PHP a million times. Give or take.
So this is what I do (to test):
> uri = '/users/#/status/*'
> uri.replace(/\//g, '\\/').replace(/#/g, '(\d+)').replace(/\*/g, '([^/]+)')
What I'm doing is 1) escaping /
and 2) replacing #
with a \d+
and 3) replacing *
with a [^/]+
In Chrome this works as expected:
< \/users\/(d+)\/status\/([^/]+)
Escaped /
and replaced #
and *
correctly. This is V8.
In Nodejs:
< \\/users\\/(d+)\\/status\\/([^/]+)
Que!? Every /
is doubly escaped? I either get a doubly escaped /
or a not escaped /
.
The regex is right, right?
I'm using Chrome 15 dev (V8 javascript) and Node 0.5.8 dev (V8 javascript). What's going on here?
Potentially interesting
If I test /^\/users\/(\d+)\/status\/([^/]+)/.test('/users/1/status/x')
it returns true in both Chrome and Node.
-
1
Where are you getting those results on node, at the mand line?, I just tried it out and it seems that on the REPL it shows the backslashes as scaped, for example,
'\\'
will show as'\\'
but the string actually consist of one character,'\\'.length == 1
. – Christian C. Salvadó Commented Sep 23, 2011 at 20:43 - Are you looking at the log in Node.js? It's possible there's logging differences. – Digital Plane Commented Sep 23, 2011 at 20:43
- What happens if you split the replaces on to multiple lines? I wonder if something is switching the order and the backslash replace is run last or in a different order? – Gates VP Commented Sep 23, 2011 at 20:44
-
@CMS I'm trying it on the CL. A string representation should be literal right? I understand
'\\'.length
, but try just'\\'
and see the response: "\" (in Chrome). So the result is literal, but the definition is escaped. I think... – Rudie Commented Sep 23, 2011 at 20:50 -
1
@CMS Okay, that was an easy test =)
'\\'
in Chrome returns"\"
, but in Node CL it returns'\\'
. The exact same thing, represented differently. I guess that part's not in V8 =) Looking for a +1? – Rudie Commented Sep 23, 2011 at 20:54
1 Answer
Reset to default 5This is due to a difference in the way the Node REPL differs from the Chrome console. When you run the mand in Node, you're getting a peek at the escaped string (including the "invisible" escape characters), but when you see it in Chrome, it's the actual string evaluated, with the escape characters removed. They're the same strings, but Chrome is trying to "prettify" it for you.
In fact, if you copy and paste the string you got from Node into the Chrome (or even a FF Firebug) console, you'll get a string with the single escapes. If you copy and paste it in again, it'll remove the next level of escapes characters.
Chrome console:
> "\\/users\\/(d+)\\/status\\/([^/]+)"
"\/users\/(d+)\/status\/([^/]+)"
> "\/users\/(d+)\/status\/([^/]+)"
"/users/(d+)/status/([^/]+)"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745354246a4624020.html
评论列表(0条)