正则表达式匹配仅在找到一个匹配项时生效。

huangapple go评论56阅读模式
英文:

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)

Here is an example used:

\.(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)

Here is an example used:

\.(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

/(?&lt;!\.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:

/(?&lt;!\.jsx?)\.jsx?$/i
/^(?!.*(?:\.jsx?){2}$).*\.jsx?$/i

Pattern details:

  • (?&lt;!\.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 = [
  &#39;test.js&#39;,
  &#39;test.jsx&#39;,
  &#39;test.ts&#39;,
  &#39;test.js.js&#39;,
  &#39;test.js.jsx&#39;,
  &#39;test.jsx.js&#39;,
  &#39;test.jsx.jsx&#39;,
  &#39;test.js.ok.js&#39;,
  &#39;test.jsx.ok.jsx&#39;,
];

files.forEach((file) =&gt; {
  const match = file.match(ALL_EXTENSION);
  const allow =
    (match &amp;&amp;
      ALLOW_EXTENSION.test(match[match.length - 1]) &amp;&amp;
      !ALLOW_EXTENSION.test(match[match.length - 2])) ||
    false;
  console.log(file, allow);
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 15:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定