Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - 正则表达式以匹配[anypath] / ui / [anyfile] .png(Regexp to match [anypath]/ui/[anyfile].png)

I have set of image paths like these:

(我有这样的图像路径集:)

a/asstes/ui/img1.png
b/nested/source/ui/img.jpg
...

idea is that path to image and it's name can me random, but important parts are that image is inside ui folder and has extension of jpg or png .

(想法是图像的路径及其名称可以随意,但重要的部分是图像位于ui文件夹中,并具有jpgpng扩展名。)

What regex can I use to check filepath for these conditions?

(我可以使用哪些正则表达式来检查这些条件的文件路径?)

Thus far I got to this point /\ui\/img1\.(png|jpg)$/ this matches a/asstes/ui/img1.png correctly, but ideally img1 should be dynamic as file name can be anything.

(迄今为止,我走到这一步/\ui\/img1\.(png|jpg)$/此相匹配a/asstes/ui/img1.png正确的,但理想img1应该是动态的文件名可以是任何东西。)

EDIT ideally it will match images that are directly under /ui

(编辑理想它将匹配直属图像/ui)

  ask by Ilja translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If /ui/ have to be the last directory, use:

(如果/ui/必须是最后一个目录,请使用:)

 var test = [ 'a/asstes/ui/img1.png', 'b/nested/source/ui/img.jpg', 'b/nested/ui/source/img.png', ]; console.log(test.map(function (a) { return a+' :'+/\/ui\/[^\/]+\.(png|jpg)$/.test(a); })); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...