-1

I want to implement a JavaScript RegEx that not ending with (.js|.jsx|.scss), execution result like below, but no operator ! before RegEx.

!/(.js|.jsx|.scss)$/
licaomeng
  • 919
  • 2
  • 13
  • 27
  • 2
    Possible duplicate of [Regex that matches anything not ending in .json](https://stackoverflow.com/questions/21962329/regex-that-matches-anything-not-ending-in-json) and [Regex for matching string not ending or containing file extensions](https://stackoverflow.com/questions/39007246) – adiga Feb 02 '19 at 12:05

2 Answers2

4

You need to use a negative look ahead with alternations to reject the strings that end with those extensions you mentioned in your post. Try this regex,

^(?!.*(?:\.jsx?|\.scss)$).*$

Demo

var arr = ['abc.js','xyz.jsx','ddd.scss','abc.jsa','xyz.jsxa','ddd.scssd'];

for (s of arr) {
console.log(s+' --> '+/^(?!.*(?:\.jsx?|\.scss)$).*$/.test(s));
}
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
0

You can use this

^(?!.*\.(?:jsx?|scss)$).*$
  • ^ - Anchor to start of string.
  • (?!.*\.(?:jsx?|scss)$).* - Condition to check jsx, js and scss
  • $ - End of string.

Demo

Code Maniac
  • 37,143
  • 5
  • 39
  • 60