I need to move a value attribute at the beginning of a html string containing tags with some other attributes.
It can pass to me something like this
<option (attrs1)* value="1" (attrs2)*>...</option>
<option (attrs1)* value='1' (attrs2)*>...</option>
<option (attrs1)* value=1 (attrs2)*>...</option>
And it should be
<option value="1" (attrs1)* (attrs2)*>...</option>
<option value='1' (attrs1)* (attrs2)*>...</option>
<option value=1 (attrs1)* (attrs2)*>...</option>
How can it be done via Regex in .Net?
- It’s a training exercise
Disclaimer: it’s a Javascript based solution, but I imagine, that .Net provides the same support for regular expressions as other languages like Python and Ruby, hence the approach should be valid (minus language specific syntax). It’s here to show that it can be done using just a single regexp.
The idea behind the regex is to find start of the tag, the “value=…” part and then everything in between. Then using replace function you reorganize found parts so the “value” tag is always just after start tag.
Ok, here goes (Javascript version):
Update: Here’s C# version (by fX’):