2

I know that there are a lot of answers to vim/regex, but I can't seem to find one that fits the bill for me. So, I'm trying to remove some attributes from HTML using the following:

%s/( id="\| onfocus="\| onBlur="\| style="\| title=") [a-zA-Z0-9:;&$_\.\s\(\)\-\,]*"//gc

I'd like to be able to replace any of the specified attributes and everything up to and included the second =.

When I run this, I get pattern not found. However, if I take the parens off of id="\| onfocus="\| onBlur="\| style="\| title=" I get a match for id= and I get a match for everything on title="hi" How can I include all of the attributes in one regex?

Edit: A result of running

%s/ id="\| onfocus="\| onBlur="\| style="\| title=" [a-zA-Z0-9:;&$_\.\s\(\)\-\,]*"//gc

will change

<input type="submit" id="submit" title="submit" value="submit" />

To

<input type="submit" submit" value="submit" />

You can see it picks up the id= but not the text and double quote after the attribute up to the next space. However, it is pattern matching the correct thing for the title attribute. In this example title="submit" has been fully removed.

1
  • 3
    it is always better to provide some few real original lines of text and example of the resulting lines expected. Could you edit the question and add this information?
    – Jay jargot
    Commented Apr 12, 2016 at 14:48

1 Answer 1

4

I found a way to do it:

 %s/\(id\|class\)="\([^"]*\)"/\2/gc
  1. You catch id and class in the first group to add the big fat or \| operator.

  2. You match ="

  3. You catch the text you want to keep in the second capture group \([^"]*\) (assuming here you want anything up to a ").

  4. You match the closing "

  5. And you leave only the second group.

You can then use \2 as you want.

1
  • I'm accepting this answer because it leads directly to what I was asking. I left out the \2 because I'd like the entire selection to be removed. However, thanks for the tip for future reference!
    – nland
    Commented Apr 12, 2016 at 15:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.