Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex replace href value between " and " for <a> not <link> tags

Tags:

regex

Using Sublime Text 2, I want to use regex to replace every href value between " and " with a new value.

Example:

<a href="link/to/somewhere.html">

convert to

<a href="#">

But the href has to have a corresponding <a> tag not within a <link> tag.

like image 427
Jason Avatar asked Jul 01 '14 23:07

Jason


2 Answers

You can use the following. Use Ctrl + H to open the Search and Replace, enable Regular Expression

Find: (<a[^>]*href=")[^"]*("[^>]*>)
Replace: $1#$2

Alternatively, you can use the \K escape sequence which is little more simpler. \K resets the starting point of the reported match and any previously consumed characters are no longer included.

Find: <a[^>]*href="\K[^"]*
Replace: #
like image 183
hwnd Avatar answered Sep 30 '22 13:09

hwnd


In Sublime Text 2:

Find What:    <a (.+ )?href="([^"]+)"([^>]+)?>
Replace With: <a \1href="#"\3>
like image 22
bloodyKnuckles Avatar answered Sep 30 '22 14:09

bloodyKnuckles