如何从带有正则表达式的URL中获取最后一部分?

时间:2019-05-23 10:05:57

标签: javascript regex

v_a.size() < v_b.max_size()我有以下URL(的一部分)。其中我需要了解详细信息     在最后的“ /”之后

v_b.resize(static_cast<std::vector<B>::size_type>(v_a.size()));

我知道如何匹配以下所有部分

enter code here

但是我需要在最后的“ /”之后获得最后一部分。

1 个答案:

答案 0 :(得分:0)

此正则表达式将为您工作:

(?<=\/)[a-z0-9-]+?$

正则表达式demo

说明:

  

(?<= )后向寻找找到开始比赛的模式(但不会在匹配组中包括该模式)。

     

\/匹配文字/

     

[a-z0-9-]与字母数字或连字符-

匹配      

+?字符必须出现一次或多次直到下一个模式

     

$输入结束

由于并非所有浏览器都支持 lookbehind ,因此我也为javascript提供了此版本:

var regex = /\/([a-z0-9-]+)$/

match = "/magasins/vos-magasins/le-bhv-marais".match(regex); 
console.log(match[1]); // le-bhv-marais

match = "/magasins/vos-magasins/le-bhv-marais-homme".match(regex);
console.log(match[1]); // le-bhv-marais-homme

match = "/magasins/vos-magasins/bhv-marais-parly2".match(regex);
console.log(match[1]); // bhv-marais-parly2

match = "/magasins/le-bhv-marais-la-niche".match(regex);
console.log(match[1]); // le-bhv-marais-la-niche

match = "/magasins/vos-magasins/mobicity-au-bhv-marais".match(regex);
console.log(match[1]); // mobicity-au-bhv-marais

match = "/magasins/vos-magasins/gucci-au-bhv-marais".match(regex);
console.log(match[1]); // gucci-au-bhv-marais

相关问题