英文:
Regexp match when only one occurence found
问题
For example, I want to check if a file has only one extension. I tried to use the groups, but they don't allow me to verify that they appear only once (I didn't find out how to do it)
\.(js|jsx)$
For example, if js and jsx are allowed:
test.js -> should match
test.jsx -> should match
test.ts -> should not match
test.js.jsx -> should not match
Any string with .js.jsx
, .jsx.jsx
, .jsx.js
or .js.js
at the end are not allowed.
英文:
For example, I want to check if a file has only one extension. I tried to use the groups, but they don't allow me to verify that they appear only once (I didn't find out how to do it)
\.(js|jsx)$
For example, if js and jsx are allowed:
test.js -> should match
test.jsx -> should match
test.ts -> should not match
test.js.jsx -> should not match
Any string with .js.jsx
, .jsx.jsx
, .jsx.js
or .js.js
at the end are not allowed.
答案1
得分: 3
If you only target modern browsers that are ECMAScript 2018+ compliant, you can use:
/(?<!\.jsx?)\.jsx?$/
For other environments that are not ES2018+ compliant, you can use:
/^(?!.*(?:\.jsx?){2}$).*\.jsx?$/
See the regex demo #1 and regex demo #2.
If you need it case insensitive, add the i
flag:
/(?<!\.jsx?)\.jsx?$/i
/^(?!.*(?:\.jsx?){2}$).*\.jsx?$/i
Pattern details:
/(?<!\.jsx?)
- a negative lookbehind that fails the match if - immediately to the left of the current location - there is a.js
or.jsx
substring\.jsx?
- a.js
or.jsx
string$
- end of string.
The ^(?!.*(?:\.jsx?){2}$).*\.jsx?$
regex matches:
^
- start of string(?!.*(?:\.jsx?){2}$)
- a negative lookahead that fails the match if there are any zero or more chars other than line break chars as many as possible and two occurrences of.js
or.jsx
at the end of the string.*
- any zero or more chars other than any line break chars as many as possible\.jsx?
-.js
or.jsx
$
- end of string.
英文:
If you only target modern browsers that are ECMAScript 2018+ compliant, you can use
/(?<!\.jsx?)\.jsx?$/
For other environments that are not ES2018+ compliant, you cna use
/^(?!.*(?:\.jsx?){2}$).*\.jsx?$/
See the regex demo #1 and regex demo #2.
If you need it case insensitive, add the i
flag:
/(?<!\.jsx?)\.jsx?$/i
/^(?!.*(?:\.jsx?){2}$).*\.jsx?$/i
Pattern details:
(?<!\.jsx?)
- a negative lookbehind that fails the match if - immediately to the left of the current location - there is a.js
or.jsx
substring\.jsx?
- a.js
or.jsx
string$
- end of string.
The ^(?!.*(?:\.jsx?){2}$).*\.jsx?$
regex matches
^
- start of string(?!.*(?:\.jsx?){2}$)
- a negative lookahead that fails the match if there are any zero or more chars other than line break chars as many as possible and two occurrences of.js
or.jsx
at the end of string.*
- any zero or more chars other than any line break chars as many as possible\.jsx?
-.js
or.jsx
$
- end of string.
答案2
得分: 0
.\w+\.\w+
正则表达式匹配具有多个扩展名的文件。但是,可能需要先进行一些字符串操作。
英文:
\.\w+\.\w+
this regex expression matches files with more than one extension. However, it may be necessary to do some string operations first.
答案3
得分: 0
以下是翻译的代码部分:
使用全局匹配来获取所有扩展名,然后比较最后两个扩展名:
匹配所有扩展名:
/\..+?\b/g
测试有效的扩展名:
/\.jsx?/
以下是示例代码:
const ALL_EXTENSION = /\..+?\b/g;
const ALLOW_EXTENSION = /\.jsx?/;
const files = [
'test.js',
'test.jsx',
'test.ts',
'test.js.js',
'test.js.jsx',
'test.jsx.js',
'test.jsx.jsx',
'test.js.ok.js',
'test.jsx.ok.jsx',
];
files.forEach((file) => {
const match = file.match(ALL_EXTENSION);
const allow =
(match &&
ALLOW_EXTENSION.test(match[match.length - 1]) &&
!ALLOW_EXTENSION.test(match[match.length - 2])) ||
false;
console.log(file, allow);
});
英文:
One way is to use a global match to get all extensions and then compare the last two extensions:
Match all extensions:
/\..+?\b/g
Test valid extensions:
/\.jsx?/
Here is a example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ALL_EXTENSION = /\..+?\b/g;
const ALLOW_EXTENSION = /\.jsx?/;
const files = [
'test.js',
'test.jsx',
'test.ts',
'test.js.js',
'test.js.jsx',
'test.jsx.js',
'test.jsx.jsx',
'test.js.ok.js',
'test.jsx.ok.jsx',
];
files.forEach((file) => {
const match = file.match(ALL_EXTENSION);
const allow =
(match &&
ALLOW_EXTENSION.test(match[match.length - 1]) &&
!ALLOW_EXTENSION.test(match[match.length - 2])) ||
false;
console.log(file, allow);
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论