[英]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.