ebbishop
ebbishop

Reputation: 1983

Javascript Regexp: match string beginning with X, excluding Y, ending with A or B

I'm trying to match single line strings that start with ./, do not contain main and do end in .js or .vue

Should match:

./test.js
./component.vue

Should not match:

./main.js
./data.json

I tried using a lookahead like this:

/^\.\/(?!main)(\.js|\.vue)$/

but that doesn't return any of the above strings.

Upvotes: 2

Views: 193

Answers (1)

anubhava
anubhava

Reputation: 785761

You may use this regex:

^\.\/(?!.*main).*\.(?:js|vue)$

RegEx Demo

RegEx Breakup:

  • ^: Start
  • \.\/: Match ./ at start
  • (?!.*main): Negative lookahead to assert we don't have main anywhere
  • .*\.(?:js|vue): Match any string that ends with .js or .vue
  • $: End

Upvotes: 3

Related Questions