Javascript regex for matching/extracting file extension

The following regex

var patt1=/[0-9a-z]+$/i;

extracts the file extension of strings such as

filename-jpg
filename#gif
filename.png

How to modify this regular expression to only return an extension when string really is a filename with one dot as separator ? (Obviously filename#gif is not a regular filename)

UPDATE Based on tvanofsson's comments I would like to clarify that when the JS function receives the string, the string will already contain a filename without spaces without the dots and other special characters (it will actually be handled a slug ). The problem was not in parsing filenames but in incorrectly parsing slugs - the function was returning an extension of "jpg" when it was given "filename-jpg" when it should really return null or empty string and it is this behaviour that needed to be corrected.


Just add a . to the regex

var patt1=/.[0-9a-z]+$/i;

Because the dot is a special character in regex you need to escape it to match it literally: . .

Your pattern will now match any string that ends with a dot followed by at least one character from [0-9a-z] .

eg

foobar.a
foobar.txt
foobar.foobar1234

if you want to limit the extension to a certain amount of characters also, than you need to replace the +

var patt1=/.[0-9a-z]{1,5}$/i;

would allow at least 1 and at most 5 characters after the dot.


Try

var patt1 = /.([0-9a-z]+)(?:[?#]|$)/i;

This RegExp is useful for extracting file extensions from URLs - even ones that have ?foo=1 query strings and #hash endings.

It will also provide you with the extension as $1 .

var m1 = ("filename-jpg").match(patt1);
alert(m1);  // null

var m2 = ("filename#gif").match(patt1);
alert(m2);  // null

var m3 = ("filename.png").match(patt1);
alert(m3);  // [".png", "png"]

var m4 = ("filename.txt?foo=1").match(patt1);
alert(m4);  // [".txt?", "txt"]

var m5 = ("filename.html#hash").match(patt1);
alert(m5);  // [".html#", "html"]

PS +1 for @stema who offers pretty good advice on some of the RegExp syntax basics involved.


Example list:

var fileExtensionPattern = /.([0-9a-z]+)(?=[?#])|(.)(?:[w]+)$/gmi
//regex flags -- Global, Multiline, Insensitive

var ma1 = 'css/global.css?v=1.2'.match(fileExtensionPattern)[0];
console.log(ma1);
// returns .css

var ma2 = 'index.html?a=param'.match(fileExtensionPattern)[0];
console.log(ma2);
// returns .html

var ma3 = 'default.aspx?'.match(fileExtensionPattern)[0];
console.log(ma3);
// returns .aspx

var ma4 = 'pages.jsp#firstTab'.match(fileExtensionPattern)[0];
console.log(ma4);
// returns .jsp

var ma5 = 'jquery.min.js'.match(fileExtensionPattern)[0];
console.log(ma5);
// returns .js

var ma6 = 'file.123'.match(fileExtensionPattern)[0];
console.log(ma6);
// returns .123

Test page.

链接地址: http://www.djcxy.com/p/46938.html

上一篇: 在Bash脚本中输入/输出剪贴板

下一篇: Javascript正则表达式匹配/解压缩文件扩展名