How to find and replace href values on links using AngleSharp?


Rays

I have some HTML snippets that contain some links with hrefs that start with a pound sign like this

<a href="#Getting Started">Getting Started</a>

I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value, then return the updated HTML markup.

Florian Rappell

The great thing about AngleSharp is that you can basically use any JS solution - because AngleSharp exposes the W3C DOM API (JS uses it too). All you need to do is replace some camelCase with PascalCase and use standard .NET tools instead of the ones in JS.

Let's take " how to change all links using javascript" as an example (sorry it was the first hit of my Google search) and use that as a starting point.

var context = BrowsingContext.New(Configuration.Default);
var document = await context.OpenAsync(res => res.Content(""));
var anchors = document.GetElementsByTagName("a");

for (var i = 0; i < anchors.Length; i++)
{
    var anchor = anchors[i] as IHtmlAnchorElement;
    anchor.Href = "http://example.com/?redirect=" + anchor.Href;
}

So in our case we are not interested in the same transformation, but very interested in similar transformations. we can do it:

for (var i = 0; i < anchors.Length; i++)
{
    var anchor = anchors[i] as IHtmlAnchorElement;

    if (anchor.GetAttribute("href")?.StartsWith("#") ?? false)
    {
        anchor.Href = "your-new-value";
    }
}

The reason is that Hrefit is always normalized (i.e. the full URL) so that the attribute value of "#foo" looks like "http://example.com/path#foo". By looking at the original value, we can just assume that the value still starts with the hash symbol.

Related


How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

Find all links and replace them with their href values

Alexander Marita I think I have a string like this: <a href="site/project/109#" target="_blank">href</a> text, text, <a href="/target" class="test">test</a> i need the output site/project/109# text, text, test I can find all the links var txt = msg.match(/\

How to replace href links in text using jQuery

Alpha reticulum cells HTML example: <p><a href='link1' target='_blank'><font color='blue' size='4'>TEXT 1</font></a></p> <br /> <p><a href='link2' target='_blank'><font color='blue' size='4'>TEXT 2</font></a></p> <br /> <p><a href='link3' target='_

How to replace href links in text using jQuery

Alpha reticulum cells HTML example: <p><a href='link1' target='_blank'><font color='blue' size='4'>TEXT 1</font></a></p> <br /> <p><a href='link2' target='_blank'><font color='blue' size='4'>TEXT 2</font></a></p> <br /> <p><a href='link3' target='_

Find and replace href values using PowerShell?

Pete Whelan I have an HTML file with lots of links. They are in the format http:/oldsite/showFile.asp?doc=1234&amp;lib=lib1I want to replacehttp://newsite/?lib=lib1&doc=1234 (1234 and lib1 are variable) Any ideas on how to do this? Thank you P follow I don't t

Find and replace href values using PowerShell?

Pete Whelan I have an HTML file with lots of links. They are in the format http:/oldsite/showFile.asp?doc=1234&amp;lib=lib1I want to replacehttp://newsite/?lib=lib1&doc=1234 (1234 and lib1 are variable) Any ideas on how to do this? Thank you P follow I don't t

How to replace href links in Textarea with jQuery

radical action I have a basic question about jQuery. I really want to change the "a" tag in Textarea. I know "a" tags don't work inside a textarea, so I can't use jQuery selectors to target them. example: <textarea><a href="http://www.google.com"></a></textare

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace href links in Textarea with jQuery

radical action I have a basic question about jQuery. I really want to change the "a" tag in Textarea. I know "a" tags don't work inside a textarea, so I can't use jQuery selectors to target them. example: <textarea><a href="http://www.google.com"></a></textare

Get url from href using AngleSharp library

David Russell I am using c# with AngleSharp library to read URL from it, <a>I can read the content easily using var items = document.QuerySelectorAll("a"); However, to read the URL from the attributes of hrefall <a>tags , what should I do ? Ahmed Soliman Try

Get Href attribute using anglesharp linq query

Guerrilla I am trying to understand how to use Anglesharp. I wrote the following code based on the example ( https://github.com/AngleSharp/AngleSharp ) : // Setup the configuration to support document loading var config = Configuration.Default.

Get Href attribute using anglesharp linq query

Guerrilla I am trying to understand how to use Anglesharp. I wrote the following code based on the example ( https://github.com/AngleSharp/AngleSharp ) : // Setup the configuration to support document loading var config = Configuration.Default.

Get url from href using AngleSharp library

David Russell I am using c# with AngleSharp library to read URL from it, <a>I can read the content easily using var items = document.QuerySelectorAll("a"); However, to read the URL from the attributes of hrefall <a>tags , what should I do ? Ahmed Soliman Try

Get Href attribute using anglesharp linq query

Guerrilla I am trying to understand how to use Anglesharp. I wrote the following code based on the example ( https://github.com/AngleSharp/AngleSharp ) : // Setup the configuration to support document loading var config = Configuration.Default.

Replace tags with href links

for J9 I have an html source and want to replace all tags with the href links it contains .<a> So the label looks like: <a href="http://google.com" target="_blank">click here</a> I expect as output: http://google.com I've tried some regex in conjunction with

Find href links with BeautifulSoup

username I am using BeautifulSoup to find href by class "reply_to". <div class="message"> <div class="reply_to details"> In reply to <a href="#go_to_message18" onclick="return GoToMessage(18)">this message</a> </div> </div> Now, the cod

Find links with href content

phadaphunk I used this code: https://stackoverflow.com/a/16209027/1063093 In my case, I wanted to select the link with the word edit in the href . IWebElement link = driver.FindElementByCssSelector("[href*='edit']"); If I remember correctly, *=check if the hr