Javascript Regex - 如何在图像路径之前提取最后一个单词 [英] Javascript Regex - How to extract last word before path to image
本文介绍了Javascript Regex - 如何在图像路径之前提取最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用正则表达式从文件路径中提取最后一个单词.例如,我有:/xyz/blahblah/zzz/abc-blah/def-xyz-颜色.jpg
I want to use regex to extract the last word from a file path. For example, I have: /xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg
我想从路径中提取颜色".路径颜色有不同的语法.唯一一致的是结尾总是 -color.jpg,其中 color 是任何 [a-z] 字.
I want to extract the "color" out of the path. The path color have different syntax. The only thing that is consistent is the ending where it is always -color.jpg where color would be any [a-z] word.
有没有一种优雅的方法来做到这一点?
Is there an elegant way to do this?
我真的很感激这里的任何帮助.谢谢
I would really appreciate any help here. Thanks
推荐答案
var matched = /-(\w+).jpg/i.exec('/xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg')[1];
这篇关于Javascript Regex - 如何在图像路径之前提取最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文