[英]get any string between last two slash or other last two same character with regex?
如何在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)
}
您可以使用此正则表达式:
/[^/]*(?=\/$)/
它会按预期输出["s", index: 7, input: "/a/b/c/s/"]
。
[^/]* # any char that is not /
(?=\/$) # Look foward for a / and the end of string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.