Regular Expressions 101

Community Library Entry

2

Regular Expression
ECMAScript (JavaScript)

/
\.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$
/
gmi

Description

var fileExtensionPatter = /.([0-9a-z]+)(?=[?#])|(.)(?:[\w]+)$/

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

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

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

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

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

var ma6 = 'file.123'.match(fileExtensionPatter)[0];
console.log(ma6);
// return .123
Submitted by Ahbap Aldırmaz - 9 years ago