繁体   English   中英

正则表达式匹配域内的子文件夹

[英]Regex to match subfolder inside of a domain

我目前正在使用\\/[^\\.]+$来匹配 url 中的子文件夹。

我想知道如何修改它以仅与子文件夹子字符串匹配。

它目前正在识别 http:// 和 https:// 协议,但我不希望它匹配。

演示: https : //regex101.com/r/nG7kM9/12

例子:

thisisatest.com => no match
https://thisiatest.com/ => no match
http://thisiatest.com/folder/folder2/ => /folder/folder2/ match
http://thisisatest.com/folder => /folder match
https:// => https:// match ( but don't want this to match )
http:// => http:// match ( but don't want this to match )

尝试这个:

(?<!\/)\/[^\/\.][^\.:]*$
  • (?<!\\/)\\/ : 匹配/但不匹配//
  • [^\\/\\.] : 不匹配. 只要你想,不匹配/字符后/
  • [^\\.:]* : 不匹配. 如你所愿,不匹配:

注意:你不想要 match . ,但我认为它可能与 URL have 错误. ,你能添加所有你想要的案例吗? 我改变正则表达式来帮助你

您可以首先将 http 协议与可选的 s 和://匹配

然后使用否定字符类[^\\/\\r\\n]+匹配除/之外的任何字符

在捕获组中匹配一个正斜杠,然后匹配除换行符以外的任何字符 1+ 次(\\/.+)以防止末尾的单斜杠匹配。

该值在第一个捕获组中

^https?:\/\/[^\/\r\n]+(\/.+)$

正则表达式演示

 const regex = /^https?:\\/\\/[^\\/\\r\\n]+(\\/.+)$/gm; const str = `thisisatest.com https://thisiatest.com/ http://thisiatest.com/folder/folder2/ http://thisisatest.com/folder https:// http:// `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } console.log(m[1]); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM