使用regex获取最后两个斜杠之间的任何字符串或其他最后两个相同字符

get any string between last two slash or other last two same character with regex?

本文关键字:两个 最后 字符串 其他 字符 任何 获取 regex 使用 之间      更新时间:2023-09-26

如何在javascript中获得最后两个斜杠或其他最后两个相同字符之间的任何字符串?

使用正则表达式不分割有几个类似的问题,所以,但我只能找到答案是使用分割…

我的正则表达式模式在下面,它不匹配,我错过了什么?

我希望结果是这样的,如何使它?

['s', index: .., input: ...]
正则表达式

var str = '/a/b/c/s/';
var regexPattern = /([^/]*)'/$/;
str = regexPattern.exec(str);
console.log(str); // ["s/", "s"]
if (str == 's') {
   console.log(true)
}
https://jsfiddle.net/30bjt5ew/

你可以使用这个正则表达式:

/[^/]*(?='/$)/

它将输出您期望的["s", index: 7, input: "/a/b/c/s/"]

[^/]* # any char that is not /
(?='/$) # Look foward for a / and the end of string

jsfiddle