Lenny D
Lenny D

Reputation: 2044

Match file paths in HTML and Javascript Files

If anyone can help me having a lot of trouble with a regex expression

Basically I need a RegEx that can spot files in html,css,javascript enclosed by single or double quotes

I have got this far (\"|')([^"|'|\s]|\\"*)*\..*(\"|')

I am using C#

See the link https://regex101.com/r/nga5yF/2

But if you look at my tests at the bottom where I have multiple matches on a single line it fails.

Any help would be appreciated!

Upvotes: 1

Views: 157

Answers (2)

wp78de
wp78de

Reputation: 18980

We can use a negated character class for this:

['"][^'" ]+?\.[^'" ]*?['"]

Online Demo

Explanation:

  • everything between quotes, regardless of type, if there is a .

Upvotes: 2

NineBerry
NineBerry

Reputation: 28509

Instead of * use the non-greedy or lazy *? quantifier to match an unlimited number of repetitions, but in a non-greedy way. (i.e. take the shortest match).

Also, you forgot to exclude whitespace and quotes in the part after requiring a dot to be included.

Test this version of the regex:

(?<quote>\"|\')(?<file>[^\"\'\s]*?\.[^\"\'\s]*?)\k<quote>

https://regex101.com/r/wTXhaM/1

Further improvements:

  • Use named capturing groups.
  • Use a back reference at end of pattern to match double quote or single quote depending on beginning of string.

Or if you want to also match filenames where single and double quotes are mixed use this variant:

(?:\"|\')(?<file>[^\"\'\s]*?\.[^\"\'\s]*?)(?:\"|\')
  • Use named capturing group for filename.
  • Use non-capturing groups for quotes

https://regex101.com/r/uM2Qfd/1

Upvotes: 1

Related Questions