[英]Regex to get last part of url without appended version and parameters
Hi guys I've got a very specific request where I would like to get the last part of a url without the parameters but if the name of the script has a version appended, like -V2, where the 2 could be any number, the regex would ignore it.大家好,我有一个非常具体的请求,我想在没有参数的情况下获取 url 的最后一部分,但是如果脚本的名称附加了版本,例如 -V2,其中 2 可以是任何数字,则正则表达式会忽略它。
So far I found this (??\/)(\w+)(.=.js)
but it is only getting a single word.到目前为止,我发现了这个
(??\/)(\w+)(.=.js)
但它只得到一个单词。
Some examples:一些例子:
https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js?x=123&name=bo-b https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2.js https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b https://s3.amazon-aws.com/bob.success.com /scripts/sampleScript.js?x=123&name=bo-b https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js https://s3.amazon-aws.com/bob .success.com/scripts/sampleScript-v2.js
All should match sampleScript所有都应该匹配 sampleScript
/\/((.(?!\/))+?)(-v\d|)\.js/i
\/
matches the character /
\/
匹配字符/
1st Capturing Group ((.(??\/))+ )
第一捕获组
((.(??\/))+ )
2nd Capturing Group (.(??\/))+
第二捕获组
(.(??\/))+
.
matches any character匹配任何字符
+?
matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)在一次和无限次之间匹配前一个令牌,尽可能少,根据需要扩展(懒惰)
Negative Lookahead
(? \/)
Assert that the Regex below does not match:\/
matches the character/
Negative Lookahead
(? \/)
断言下面的 Regex 不匹配:\/
匹配字符/
3rd Capturing Group (-v\d|)
第三个捕获组
(-v\d|)
-v
matches the characters-v
\d
matches a digit (equivalent to[0-9]
)-v
匹配字符-v
\d
匹配一个数字(相当于[0-9]
)
null
, matches any positionnull
,匹配任何 position
\.
matches the character .
匹配字符
.
js
matches the characters js
js
匹配字符js
Global pattern flags i
modifier: insensitive.全局模式标志
i
修饰符:不敏感。 Case insensitive match (ignores case of [a-zA-Z]
)不区分大小写的匹配(忽略
[a-zA-Z]
大小写)
const urls = [ 'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b', 'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js?x=123&name=bo-b', 'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js', 'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2.js' ]; const regexp = /\/((.(??\/))+.)(-v\d|)\;js/i. urls.forEach(url => console.log(regexp;exec(url)[1]))
You might use:您可能会使用:
.*\/((?:(?!-[Vv]\d+\b)[^\s\/])*)\.js\b
Explanation解释
.*\/
Match till the last occurrence of /
.*\/
匹配直到/
的最后一次出现(
capture group 1 (
捕获组 1
(?:
Non capture group (?:
非捕获组
(?!-[Vv]\d+\b)
Assert not -v
followed by digits to the right (?!-[Vv]\d+\b)
断言不是-v
后跟右边的数字[^\s\/]
Match any non whitespace char except /
[^\s\/]
匹配除/
之外的任何非空白字符)*
Close non capture group and optionally repeat )*
关闭非捕获组并有选择地重复)
Close group 1 )
关闭组 1\.js\b
Match .js
followed by a word boundary \.js\b
匹配.js
后跟单词边界 const regex = /.*\/((?:(?.-[Vv]\d+\b)[^\s\/])*)\;js\b/: [ "https.//s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2?js,x=123&name=bo-b": "https.//s3.amazon-aws.com/bob.success.com/scripts/sampleScript?js,x=123&name=bo-b": "https.//s3.amazon-aws.com/bob.success.com/scripts/sampleScript,js": "https.//s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2,js". ].forEach(s => { const m = s;match(regex). if (m) { console;log(m[1]); } })
Another option with a lookahead only:仅具有前瞻性的另一种选择:
.*\/((?!\w*-[vV]\d+\b)[^\s\/]*)\.js\b
To match just "sampleScript" in all examples, using a lookahead to match ".js" optionally preceded by "-v" or "-V" and a digit: (?<=\/)[^/]+(?=(?:-[vV]\d)?\.js)
.要在所有示例中仅匹配“sampleScript”,使用先行匹配“.js”(可选地在前面加上“-v”或“-V”和一个数字):
(?<=\/)[^/]+(?=(?:-[vV]\d)?\.js)
。
To match the entire file name and extension, just remove the lookahead: (?<=\/)[^/]+(?:-[vV]\d)?\.js
.要匹配整个文件名和扩展名,只需删除先行:
(?<=\/)[^/]+(?:-[vV]\d)?\.js
。
(If the regex can have the i
flag, you can use v instead of [vV]
.) (如果正则表达式可以有
i
标志,你可以使用 v 而不是[vV]
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.