Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  193] [ 2]  / answers: 1 / hits: 18060  / 11 Years ago, thu, october 10, 2013, 12:00:00

Hello i need to match filnames with extensions.



Problem is that paths could be both unix and windows, so seperated by / or also unix allows . in filenames, so t.est.txt also should be matched.



My code :



var regex = new RegExp('[\/]?([/w+.]+/w+)/s*$');
var value = this.attachment.fileInput.dom.value;
console.log(value.match(regex));
console.log(regex.exec(value));


this regex works fine in rubular. But for some reason ie, chrome and firefox does not match any string and returns null.


More From » regex

 Answers
70

You could just grab whatever's at the end following the last or /, such as:



var file = str.match(/[^\/]+$/)[0];


(Remember files don't always need extensions)



Though if you really want to force extension matching:



var file = str.match(/[^\/]+.[^\/]+$/)[0];

[#75100] Wednesday, October 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;