jQuery& RegEX解析图像名称 [英] jQuery & RegEX to parse image name
问题描述
我正在尝试编写一个jQuery代码段,该代码段将解析包含宽度,高度和扩展名的图像名称,并将这些值分配给变量.
I am trying to write a jQuery snippet of code, that will parse an image name containing width, height, and extensions and assign those values to variables.
图片名称格式:DSCN0551-130x130.jpg
(名称-宽度xheight.extension)
Image Name Format: DSCN0551-130x130.jpg
(name-widthxheight.extension)
我想尝试拉动我们的宽度,高度和扩展名,并将它们分配给变量.
I want to try and pull our the width, the height, and the extension and assign them to variables.
推荐答案
var str = 'DSCN0551-130x130.jpg';
var matches = str.match(/(.+)-(\d+)x(\d+)\.(\w+)$/);
var name = matches[1];
var width = matches[2];
var height = matches[3];
var extension = matches[4];
通过在末尾添加锚点,基本上可以从右到左读取正则表达式.然后在与您的结构相关的最左边的-
之后,.+
匹配剩下的任何内容(无论什么字符).
By adding the anchor to the end, you can basically read the regex from right to left. And then after the leftmost -
that is relevant to your structure, .+
matches anything that's left (no matter what characters).
这篇关于jQuery& RegEX解析图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!