Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regular expressions on vim to fnd and replace all href links content to '#'?

Tags:

regex

vim

macvim

Is it possible to use vim to replace the 'http://google.com/' and other links to '#'?

 <a class="link1" href="http://google.com/">My Link</a>
 <a class="link1" href="http://yahoo.com/">My Link</a>
 <a href="http://stackoverflow.com/">My Link</a>

to

 <a class="link1" href="#">My Link</a>
 <a class="link1" href="#">My Link</a>
 <a href="#">My Link</a>

Thanks

like image 619
user1530249 Avatar asked Dec 14 '25 06:12

user1530249


1 Answers

This one should handle the basic http case:

:%s/http:\/\/[^\"]*/#/g

But this should be more flexible - the general idea is to find instances of href=", save that as a group, then match until we hit another ". Then we replace it with our group (\1) and the hash sign:

:%s/\(href=\"\)[^\"]*/\1#/g
like image 76
RocketDonkey Avatar answered Dec 16 '25 22:12

RocketDonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!