开发者

convert javascript regex to php

开发者 https://www.devze.com 2022-12-16 21:28 出处:网络
I have the following regular expression: RegExp(\"http://www.amazon.com/([\\\\w-]+/)?(dp|gp/product)/(\\\\w+/)?(\\\\w开发者_开发技巧{10})\");

I have the following regular expression:

RegExp("http://www.amazon.com/([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w开发者_开发技巧{10})");

Written in javascript.

How can I get it to work with PHP's preg_match_all? What are the differences as far as regular expressions go?


This should work:

preg_match_all('#http://www.amazon.com/([\w-]+/)?(dp|gp/product)/(\w+/)?(\w{10})#', $data, $matches);

When you use the JavaScript RegExp constructor with a string, all backslashes have to be escaped with another backslash. This is not the case with PHP.

And normally you would use / as the delimiter for the regex, but as in this case you have a lot of / in your search string it is wiser to use some other delimiter so you don't have to escape every slash with a backslash. In this case I used the pound sign as a delimiter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号