Using .replace to replace html tags

So I have a string that looks like this: <a rel="nofollow" href="example.com" class="">1,300 Sales</a> and I need to get the number of sales. I tried to do it like this:

 sales_string = sales_string.replace(" Sales</a></span>", "").replace(
                &format!(r#"<a rel="nofollow" href="{}" class="">"#, url_href),
                "",
            );

And it works, but I think there is a better way of doing this.

You shouldn't treat HTML as text. Instead, parse it.

There are various libraries that parse it and allow extracting data from the parsed DOM tree:

7 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.