Javascript 正则表达式匹配目录,后跟点

Javascript 正则表达式匹配目录,后跟点,javascript,regex,Javascript,Regex,是否有一个JavaScript正则表达式可以匹配后跟一个点的目录?基本上,我希望匹配文件,但不匹配目录,类似于: /foo/bar/baz # does not match /foo/bar/baz.js # matches /foo/bar/baz.js/foo # does not match 这可能吗?您可以尝试以下功能 功能检查(路径){ 返回路径.split('/').pop().indexOf('.')>-1; } log(检查('/foo/bar/baz.js')//真的 lo

是否有一个JavaScript正则表达式可以匹配后跟一个点的目录?基本上,我希望匹配文件,但不匹配目录,类似于:

/foo/bar/baz # does not match
/foo/bar/baz.js # matches
/foo/bar/baz.js/foo # does not match

这可能吗?

您可以尝试以下功能

功能检查(路径){
返回路径.split('/').pop().indexOf('.')>-1;
}
log(检查('/foo/bar/baz.js')//真的
log(检查('/foo/bar/baz')//假的

log(检查('/foo/bar/baz.js/foo')//false
您可以尝试以下功能

功能检查(路径){
返回路径.split('/').pop().indexOf('.')>-1;
}
log(检查('/foo/bar/baz.js')//真的
log(检查('/foo/bar/baz')//假的

log(检查('/foo/bar/baz.js/foo')//false
我想到了这样的事情:

/^\/?([^\/]+\/)+[^\/.]+\.[^\/.]+$/g
细分:

^ start of the string
\/? start with an optional slash
()+ a group that can repeat any amount
    [^\/]+ anything thats not a slash repeated any amount
    \/ a slash
then, the final bit must b
[^\/.]+ any non slash, non dot character, repeat any times
a single dot
[^\/.]+ any non slash, non dot character, repeat any times
$ end of string

我想到了这样的事情:

/^\/?([^\/]+\/)+[^\/.]+\.[^\/.]+$/g
细分:

^ start of the string
\/? start with an optional slash
()+ a group that can repeat any amount
    [^\/]+ anything thats not a slash repeated any amount
    \/ a slash
then, the final bit must b
[^\/.]+ any non slash, non dot character, repeat any times
a single dot
[^\/.]+ any non slash, non dot character, repeat any times
$ end of string
有一种可能性:

/\.(\w)*$/g
(警告:未测试)

有一种可能性:

/\.(\w)*$/g
(警告:未测试)

尝试以下操作:

const isAFileInDirectory=str=>/[^\.]\.[^\.\/]+$/.test(str);
log(isAFileInDirectory('/foo/bar/baz');
log(isAFileInDirectory('/foo/bar/baz.js');
log(isAFileInDirectory('/foo/bar/baz.js/foo')请尝试以下操作:

const isAFileInDirectory=str=>/[^\.]\.[^\.\/]+$/.test(str);
log(isAFileInDirectory('/foo/bar/baz');
log(isAFileInDirectory('/foo/bar/baz.js');

log(isAFileInDirectory('/foo/bar/baz.js/foo')以下是我使用的:
/^[^\.]+\.[^/\.]+$/gi

console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz'));
console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz.js'));

console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz.js/foo'))以下是我使用的:
/^[^\.]+\.[^/\.]+$/gi

console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz'));
console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz.js'));

console.log(/^[^\.]+\.[^/\.]+$/gi.test('/foo/bar/baz.js/foo'))
那不是正则表达式。哦,很抱歉错过了那不是正则表达式。哦,很抱歉错过了那不是正则表达式。正则表达式末尾的
$
是做什么的?它匹配字符串的结尾。字符串的结尾。。。您的意思是空字符
\0
?不,我最明确的意思是
$
正则表达式末尾的
$
有什么作用?它与字符串结尾匹配。字符串结尾。。。你是说空字符
\0
吗?不,我最明确的意思是
$
更可靠的方法是在
[^\/]+\.^\/]+$
上使用
测试
[^\/]+\.^\/]+$
上使用
测试