的path module of nodejs提供了更好的正則表達式:
var splitDeviceRe =
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/;
// Regex to split the tail part of the above into [*, dir, basename, ext]
var splitTailRe =
/^([\s\S]+[\\\/](?!$)|[\\\/])?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/\\]*)?)$/;
function splitPath(filename) {
// Separate device+slash from tail
var result = splitDeviceRe.exec(filename),
device = (result[1] || '') + (result[2] || ''),
tail = result[3] || '';
// Split the tail into dir, basename and extension
var result2 = splitTailRe.exec(tail),
dir = result2[1] || '',
basename = result2[2] || '',
ext = result2[3] || '';
return [device, dir, basename, ext];
}
function extname(path) {
return splitPath(path)[3];
}
什麼是'avi.conn.mp3'所需輸出?你想只提取'mp3'或'conn.mp3'嗎?我嘗試了你的正則表達式,並且(一旦我刪除了空格)它就從'avi.conn.mp3'返回了'mp3'。 – nnnnnn 2012-02-26 06:47:01
它工作正常,只是在正則表達式中刪除不必要的空格。試一試'alert('avi.conn.mp3'.replace(/^.*?\。([A-zA-Z0-9] +)$ /,「$ 1」))' – Cheery 2012-02-26 06:49:34