简体   繁体   中英

Regex in Javascript; string between last and one before last “/”

How to get "foo19" out of the following string

"C:\Users\test\AppData\Roaming\test2\storage\foo19\text.txt"

lastIndexOf does not help in this case either; I just want "foo19" which happens between two "\\"s the last "\\" and the one before last "\\". Is it possible with regex?

Assuming foo19 is always the 2nd to last entry:

var str = "C:\\Users\\test\\AppData\\Roaming\\test2\\storage\\foo19\\text.txt";
var parts = str.split("\\");
var foo19 = parts[parts.length - 2];

Here is a regex that capture foo19 in a group, so access with $1.

.*\\(\w*)\\\w*.txt

The proof is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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