This is a "div" with quotation marks...">

Regular expression to replace quotes in HTML tags only

I have the following line:

<div id="mydiv">This is a "div" with quotation marks</div>

I want to use regular expressions to return the following:

<div id='mydiv'>This is a "div" with quotation marks</div>

Notice how the id attribute in the div is now surrounded by apostrophes?

How can I do this with regex?

Edit: I'm not looking for a magic bullet to handle every edge case in every situation. We all need to get tired of using regular expressions to parse HTML, but in this particular case and for my specific need, regex is the solution ... I just need a little help getting the right expression.

Edit # 2: Jens helped find a solution for me, but any random access to this page should take a long, long time thinking about using this solution. In my case, this works because I am very confident in the types of strings I will deal with. I know the dangers and risks and try to make you do it. If you are not sure whether you know, this probably indicates that you do not know and should not use this method. You have been warned.

+3
source share
3 answers

This can be done as follows: I think you want to replace every instance "that is between <and a >c '.

, " , < >. :

(?<=\<[^<>]*)"(?=[^><]*\>)

, , Regex.Replace.

. Qaru , Regex/HTML , , . , : " HTML ".

+3

:

(<div.*?id=)"(.*?)"(.*?>)

:

$1'$2'$3
0

I see that you are aware of the dangers of using Regex for such replacements. I added the following answer for those who are looking for a method that is much more "stable" if you want to have a solution that will continue to work when the input documents change.

Using the HTML flexibility package ( project page , nuget ), this does the trick:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("your html here"); 
// or doc.Load(stream);

var nodes = doc.DocumentNode.DescendantNodes();

foreach (var node in nodes)
{
    foreach (var att in node.Attributes)
    {
         att.QuoteType = AttributeValueQuote.SingleQuote;
    }
}

var fixedText = doc.DocumentNode.OuterHtml;
//doc.Save(/* stream */);
0
source

Source: https://habr.com/ru/post/1737005/


All Articles