c# - move value attribute on option tag -
i need move value attribute @ origin of html string containing tags other attributes.
it can pass me this
<option (attrs1)* value="1" (attrs2)*>...</option> <option (attrs1)* value='1' (attrs2)*>...</option> <option (attrs1)* value=1 (attrs2)*>...</option>
and should be
<option value="1" (attrs1)* (attrs2)*>...</option> <option value='1' (attrs1)* (attrs2)*>...</option> <option value=1 (attrs1)* (attrs2)*>...</option>
how can done via regex in .net?
it's training exercise
disclaimer: it's javascript based solution, imagine, .net provides same back upwards regular expressions other languages python , ruby, hence approach should valid (minus language specific syntax). it's here show can done using single regexp.
the thought behind regex find start of tag, "value=..." part , in between. using replace function reorganize found parts "value" tag after start tag.
ok, here goes (javascript version):
// illustration string var x = "<something bla=5432 other-st='asdf' value=\"45\"/><p name=asdf value=55fs andalso=\"something\">html like</p>"; x.replace(/(\<(?!\/)[a-z]+)(.+?)?(\ value=(?:\"|\')?[^\"\'\ ]+(?:\"|\')?)/gi, function(a, b, c, d) {return b+d+c;})
update: here's c# version (by fx'):
string x = "<something bla=5432 other-st='asdf' value=\"45\"/><p name=asdf value=55fs andalso=\"something\">html like</p>"; var r = new regex("(<(?!/)[a-z]+)(.+?)?(\\svalue=(?:\"|')?[^\"'\\s]+(?:\"|')?)", regexoptions.ignorecase); string s = r.replace(x, (match) => { homecoming match.groups[1].value + match.groups[3].value + match.groups[2].value; });
c# html regex
No comments:
Post a Comment