4

Looks like it is not first question about look-behind, but I didn't find an answer.

Javascript has no (positive|negative)look-behind requests.

I need a regular expression that match *.scss file name, but didn't match the names like *.h.scss. With look-behind request is looks like:

/(?<!(\.h))\.scss$/

How can I do this in javascript? I need this regular expression for webpack rules "test" parameter, so the javascript regex required only.

Jonik
  • 1,158
  • 2
  • 10
  • 20

2 Answers2

5

You may use

/^(?!.*\.h\.scss$).*\.scss$/

See the regex demo

Details

  • ^ - start of string anchor
  • (?!.*\.h\.scss$) - a negative lookahead failing the match if the string ends with .h.scss
  • .* - any 0+ chars as many as possible
  • \.scss - a .scss substring at the...
  • $ - end of the string.
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
4

You can list all possible subpatterns that doesn't match .h and build an alternation:

/(?:[^.].|\.[^h]|^.?)\.scss$/
Casimir et Hippolyte
  • 85,718
  • 5
  • 90
  • 121
  • Thank you for reply, this working solution too, but answer of [Wiktor Stribiżew](https://stackoverflow.com/users/3832970/wiktor-stribi%c5%bcew) is more simplier to understand. – Jonik Feb 11 '18 at 12:41