Ferdossi
Ferdossi

Reputation: 87

Regular expression-exclude and include some words

I have multiple files with these kind of imports:

<link rel="stylesheet" href="../vendor/pickadate/lib/themes/default.css">
<link rel="stylesheet" href="../external/default.date.css">
<script type="text/javascript" src="scheduleCtrl_Helper_5.js"></script>
 <script type="text/javascript" src="agenda_Helper.js"></script>

I want to find all imports that end with .js but not _5.js (5 or any number)

and also ignore imports from vendor and external.

I have this regexp which seems to work fine:

^(?!.external).[^\d].js".*$

so it excludes import containing external and those ending with a number.js

but how can I add multiple exclusion words ? something like:

^(?!.external|vendor).[^\d].js".*$

Thx for your help

Upvotes: 1

Views: 547

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

You can use

\b(?:src|href)="((?![^"]*(?:external|vendor))[^"]*[^"\d]\.js)"

See the regex demo.

Details

  • \b - a word boundary
  • (?:src|href) - either src or href
  • =" - a =" string
  • ((?![^"]*(?:external|vendor))[^"]*[^"\d]\.js) - Group 1:
    • (?![^"]*(?:external|vendor)) - a negative lookahead that fails the match if there are zero or more chars other than " as many as possible followed with either external or vendor
    • [^"]* - zero or more chars other than "
    • [^"\d] - a char other than " and a digit
    • \.js - a .js substring
  • " - a " char.

Upvotes: 2

Related Questions