0

I have a regular expression

\(\'?.*(\/public\/images.*[^\'"])\'?\)

It matches ulrs in my CSS file. Here is examples:

  1. background: url(/public/images/new_layout/bg-nav-active.png) no-repeat 100% 100%;
  2. background: url(/public/images/new_layout/bg-nav-active.png?v=23423h423lj4h23l4jk23hl4jkh4h2kljh) no-repeat 0 -

But, how to exclude parameter v from URL ?v=23423h423lj4h23l4jk23hl4jkh4h2kljh

Thank You for help.

1
  • 2
    What you mean by exclude? You mean second case, It should not return in the result? Commented Mar 18, 2015 at 10:55

3 Answers 3

2

Something like this should suffice;

\(\'?.*(\/public\/images[^\'"\?\)]+)

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

This will match the string "/public/images/" and everything that isn't in the character '"?)

Sign up to request clarification or add additional context in comments.

Comments

1

This regex will match just the URLs, without any parentheses or ?vs due to the a lookbehind and a lookahead:

 (?<!\()['"]?.*(\/public\/images[^'"\?\)]+)

The non-greedy .+? makes it possible not to match ?vs.

See example here.

The output of your 2 input strings is:

/public/images/new_layout/bg-nav-active.png
/public/images/new_layout/bg-nav-active.png

5 Comments

This may capture closing quotes.
Now, it won't capture closing quotes.
@ʰᵈˑ Who would be using inconsistent quotation marks like in background: url('/public/images/new_layout/bg-nav-active.png")?
.. even without inconsistent quotation marks: regex101.com/r/oK1fP8/2 (I rushed the fiddle, hence the inconsistency). You need to add " in your character class to not match [^].
@ʰᵈˑ: Agreed. It is the OP that misled me, since only single quotes are allowed in the beginning. I added the ['"] character class to also allow double quotes.
-2
str_replace('v=', '', $string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.