在Javascript中查找URL中的基本名称 [英] Find base name in URL in Javascript
问题描述
我想从Javascript中的图片网址中提取基本名称。有人会关心给我一个关于正则表达式吗?
I want to extract the base name from a image URL in Javascript. Would somebody care to give me a a hand on the Regex?
规则是:
-
返回最后
左边的最后一个
/
的所有内容。
return everything right of the last
/
left of the last.
www.domain.com/images/ image.hires .jpg
www.domain.com/images/image.hires.jpg
如果没有找到。
,返回完整的基本名称
www.domain.com/images/image_hi_res
if no .
is found, return the full base name
www.domain.com/images/image_hi_res
我有一个笨拙的 / \ /[^.]+ \。[^。] + $ /
但不知道如何制作 /
和。
可选以及如何寻找仅限最后一个。
。
I have a clumsy /\/[^.]+\.[^.]+$/
but don't know how to make the /
and the .
optional and how to seek for the last .
only.
一如既往,为所有伟大的输入欢呼。我选择了一个开箱即用的正则表达式。
Cheers folks for all the great input, as always. I chose the one Regex that worked for me out of the box.
推荐答案
又一个解决方案:
url.replace(/^.*\/|\.[^.]*$/g, '')
这篇关于在Javascript中查找URL中的基本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!