Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove single attribute with quotes via RegEx

Tags:

regex

I am trying to match an attribute to I can do a search/replace. I am having trouble though because it is matching beyond the quotes of the attribute I want. For example, I want to remove xref="..." from here:

<a href="page.ashx" xref="somethingelse" title="something" class="image">

But when I do a RegEx like this: xref=\".*\", then it selects the attributes xref, title, AND class. How do I tell it to only select the xref attribute?

like image 983
TruMan1 Avatar asked Dec 21 '22 12:12

TruMan1


2 Answers

I strongly suggest using something other than regex for modifying markup, however, this should work:

xref="[^"]*"
like image 164
Daniel Haley Avatar answered Jan 01 '23 10:01

Daniel Haley


Use the non-greedy version: \".*?\"

.* is greedy selects as much as possible. By adding a ? to it becomes less greedy selecting just as much as needed.

like image 29
Máthé Endre-Botond Avatar answered Jan 01 '23 11:01

Máthé Endre-Botond