hcharge
hcharge

Reputation: 1246

Find in files and wrap with tag - Sublime

I have a number of links across my site that I'd like to wrap with some tags, I can find these links easily enough using RegEx, but when I go to replace with tags surrounding them I'd like the original href's to remain.

So I can find them like this:

<a class="button-link" (.+)>Back</a>

This will return a number of results like this:

   96  <div class="form-group">
   97    <a class="button" href="RegistrationSubmit.html">Save and continue</a>
  *98:   <a class="button-link" href="../employer.html">Back</a>*
   99  </div>
   100  

But then if I put in the replace area:

<p>
  <a class="button-link" (.+)>Back</a>
</p>

That will simply replace every instance with the above code. What should I put in the "Replace" area? At present I have this:

enter image description here

Thanks very much

Upvotes: 1

Views: 234

Answers (1)

Robin
Robin

Reputation: 9644

  1. Don't use <a class="button-link" (.+)> to select your tag. Try to match that against

    <a class="button-link" >Next</a><a class="button-link">Back</a>
    

    and you'll see why.

  2. Regexes aren't the best tool to parse HTML, but if you're careful and the usage is limited to easy input and manual use, you could use

    <a class="button-link" ([^>]+)>Back</a>
    

    and replace with

    <p>
      <a class="button-link" \1>Back</a>
    </p>
    

You are capturing the href in the first capturing group: to call it back in the replace the syntax is \1.

[^>] means "any character but a >" and will prevent the regex to "get out" of the current tag.

Upvotes: 1

Related Questions