在堆栈上进行了一些搜索,但是找不到我的问题的答案...
我正在寻找一个正则表达式字符串,该字符串可为我的hotjar实验提取以下网址。我不确定如何使它工作。
/vacatures
/vacatures/
/vacatures/bouw/
/vacatures/installatietechniek/
但不是
/vacatures/bouw/everything-that-comes-after-the-third-slash
你们能帮我吗?
非常感谢!
答案 0 :(得分:-1)
您不需要使用正则表达式:您只需要用/
分割,然后从数组中过滤出空元素。当数组包含3个或更多项时,您知道它包含三个斜杠:
const urls = [
'/vacatures', // accept
'/vacatures/', // accept
'/vacatures/bouw/', // accept
'/vacatures/installatietechniek/', //accept
'/vacatures/bouw/everything-that-comes-after-the-third-slash', // reject
'/vacatures/bouw/everything-that-comes-after-the-third-slash/' // reject
];
function check(url) {
const parts = url.split('/').filter(x => !!x);
if (parts.length > 2) {
console.log(url, 'REJECT');
} else {
console.log(url, 'ACCEPT');
}
}
urls.forEach(check);