你能帮我解决一下regexp吗?

时间:2012-01-04 10:48:58

标签: javascript html regex url

当我想使用正则表达式时,我遇到问题,我不知道使用'或','和'。就像在if语句中一样。

我有这样的网址

http://localhost:85/study/list

我正在使用单词(pattern to regexp)/,/list/top

当我尝试使用此

url ="http://localhost:85/study/list"; 
regexp = RegExp(.*/?/list?/top);
var matches =  url.match(regexp); 
alert(matches);

当我更改网址to http://localhost:85/top时,/ top未检测到,但/ list是检测。怎么了?

我想在那个网址中匹配我的话,我必须为此创建正则表达式? 请告诉我你的答案,请给我参考regexp 我是新手,感谢您的帮助...

3 个答案:

答案 0 :(得分:1)

将它们放在括号中,例如(/list)?

答案 1 :(得分:1)

列表?匹配lis或列表。我想你想要(list)?

答案 2 :(得分:1)

伙计,你可以在这里测试你的表达RegexPal。看来你使用havo的表达没有效果。

此表达式将匹配您网址的最后一个子目录

(\w*)$

例如http://localhost:85/study/list将与list匹配,http://localhost:85/top仅匹配top

试试吧! =)