PDA

View Full Version : Regular expressions - filename filtering


Jerome
08-16-2003, 12:43 PM
Hi,

Example:

I have a string like a path:

str='folder1/vt123456.txt';

or

str='folder1/folder2/vt124.txt';

The string and filename can be any length, but the filename always start with "vt".

Question:

After using the str.replace methode (regex) the result should be the filename (without .txt) so for the above mentioned examples:

str='vt123456';

or

str='vt124';

Thanks for Your effort,
Jerome

Graeme Hackston
08-16-2003, 12:56 PM
Is there a reason you need to use replace()?

str=str.substring(str.lastIndexOf('/')+1, str.lastIndexOf('.'))

cheesebag
08-16-2003, 11:23 PM
var objRegExp = /^[\w\/]+\/(vt\w+)\.\w+$/;
str = 'folder1/vt123456.txt';
str = str.replace(objRegExp, '$1');
alert(str);
str = 'folder1/folder2/vt124.txt';
str = str.replace(objRegExp, '$1');
alert(str);