I have an HTML string. I need to change the string to add links to all header tags. For example,
First HTML
<h1> Title 1 </h1>
<p>Lorem ipsum dolor...</p>
<h2> Title 2 </h2>
<h2> Title 2 Different </h2>
The HTML I Want
<div><a class="header_link" href="#my_slugged__link_by_title"> <h1> Title 1 </h1> </a> </div>
<p>Lorem ipsum dolor...</p>
<div><a class="header_link" href="#my_slugged__link_by_title_2"> <h2> Title 2 </h2> </a> </div>
<div><a class="header_link" href="#my_slugged__link_by_title_2_different"> <h2> Title 2 Different </h2> </a> </div>
**my_slugged__link_by_title** --> I would like to create hash permalinks by Titles. (h1, h2, ...)
For example, the newdescription is my HTML string.
//Replace titles for adding backlinks
newDescription = "<h2> TEST </h2> <h2> TEST 2</h2>";
This is worked for one string
var oneTitle = Regex.Match(newDescription, @"<h2> (.+?)</h2>").Groups[1].Value
How can I replace and get all of them?
foreach (var item in Regex.Match(newDescription, @"<h2> (.+?)</h2>").Groups)
{
string header_link = "<div class=\"blog_header__backlink_item\"><a href=\"#" + item + "\"><i class=\"fas fa-link\"></i></a></div>";
newDescription = newDescription.Replace("<h2>", header_link + "<h2>");
}