正则表达式需求:匹配.scss前无-map的文件名及问题求助
Fixing Your SCSS Filename Regex
Let's break down why your original regex [a-zA-Z]+(?!-map).scss isn't working, then fix it with reliable solutions.
What's Wrong With the Original Regex?
- Missing full-filename anchors: Without
^(start) and$(end) markers, your regex matches substrings inside filenames instead of the whole thing. For example, insomething-map.scss, it will latch onto themap.scssportion, causing false positives. - Too restrictive character set:
[a-zA-Z]+only matches letters, so it can't handle hyphens in filenames likesomething-something.scss—it stops at the first hyphen and fails to match the full filename.
Working Solutions
Option 1: Negative Lookahead (Most Universal)
This regex explicitly excludes filenames ending with -map.scss while ensuring they still end with .scss:
^(?!.*-map\.scss$).*\.scss$
^anchors the match to the start of the filename(?!.*-map\.scss$)is a negative lookahead that blocks any filename ending with-map.scss.*\.scss$matches any characters followed by.scssat the end of the filename
Option 2: Negative Lookbehind (For Engine-Specific Use)
If your regex tool supports fixed-length lookbehinds (like JavaScript, Python, or grep with -P), this checks directly that .scss isn't preceded by -map:
.*(?<!-map)\.scss$
.*matches all characters leading up to the end of the filename(?<!-map)is a negative lookbehind that verifies the 4 characters before.scssaren't-map\.scss$ensures the filename ends with.scss
Testing Against Your Examples
something-something.scss: Both regexes match ✅something-map.scss: Both regexes do not match ✅something.scss: Both regexes match ✅
内容的提问来源于stack exchange,提问作者user12023283
相关产品推荐
相关产品推荐

