简体   繁体   中英

grab specific parts of a filename with jquery

I need to grab two chunks of a string to obtain the values. I've wrapped them in curly braces for demo purposes. I assume I need to use a bit of regex for this? I'm not asking for someone to spoonfeed me the code, but I'm struggling to figure out how to match more than one part of a string. Once I understand how to do this the world is my oyster.

/path/to/image/{name}-sm{1}.jpg

As you can see, I need to grab {name} and {n} ( 1 in this case ).

Like this? :)

/path/to/image/(name:\w+)-sm(id:\d+)\.jpg

Here's a small javascript snippet you can use as a guide:

>>> re = /(\w+)-sm(\d+)\.jpg/
>>> text = "/path/to/image/name-sm1.jpg"
>>> re.exec(text)

The output you get is:

["name-sm1.jpg", "name", "1"]

Another example:

>>> text = "/path/to/image/namefff-sm12.jpg"
>>> re.exec(text)
["namefff-sm12.jpg", "namefff", "12"]

Hope you find this useful...

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