javascript - String.replace withandin Nodejs different than in Chrome? - Stack Overflow

I'm trying to make a router in Nodejs. A big part of that is URI -> action, so I'll need a

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.

Share Improve this question edited Sep 25, 2011 at 12:55 thejh 45.6k18 gold badges98 silver badges108 bronze badges asked Sep 23, 2011 at 20:38 RudieRudie 54k42 gold badges135 silver badges175 bronze badges 7
  • 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
 |  Show 2 more ments

1 Answer 1

Reset to default 5

This 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信