/^(.+)(\.[^ .]+)?$/
Указанный выше шаблон неверен - он всегда будет также включать расширение. Это из-за того, как работает механизм регулярных выражений javascript. Маркер (\.[^ .]+)
является необязательным, поэтому движок успешно сопоставит всю строку с (.+)
http://cl.ly/image/3G1I3h3M2Q0M
Вот мое проверенное решение регулярных выражений.
Шаблон будет соответствовать filenameNoExt с / без расширения в пути, соблюдая разделители как косой, так и обратной косой черты
var path = "c:\some.path/subfolder/file.ext"
var m = path.match(/([^:\\/]*?)(?:\.([^ :\\/.]*))?$/)
var fileName = (m === null)? "" : m[0]
var fileExt = (m === null)? "" : m[1]
рассечение вышеуказанного рисунка:
([^:\\/]*?) // match any character, except slashes and colon, 0-or-more times,
// make the token non-greedy so that the regex engine
// will try to match the next token (the file extension)
// capture the file name token to subpattern \1
(?:\. // match the '.' but don't capture it
([^ :\\/.]*) // match file extension
// ensure that the last element of the path is matched by prohibiting slashes
// capture the file extension token to subpattern \2
)?$ // the whole file extension is optional
http://cl.ly/image/3t3N413g3K09
http://www.gethifi.com/tools/regex
Это будет охватывать все случаи, которые были упомянуты @RogerPate, но также включают полные пути