Strange question, but I won’t waste time explaining why I need to do this, just that I need to do it.
I have the following:
<input type="radio" name="eq_9" id="eq_9_2" onclick="nextStepID_load(this);" value="Installer." title="912" /><label for="eq_9_2">Installer</label> <br />
I need to turn that into:
<button type="button" name="eq_9" id="eq_9_2" onclick="nextStepID_load('912');">Installer</button><br />
I am using C#/asp.net (3.5 or below), and javascript for the performJS(which is a placeholder until I figure out how to replace the html).
Please note, the source providing this is sending me a string with the MANY rows of the inputs. And I need to replace each row with the info that is valid for it.
Right now, I’ve tried adding a .Replace(“”,”\”>”); which does replace the radio tags, but obviously makes it look horrible codewise, and doesn’t remove the label or put the label contents in between the tags.
I’m sure this is probably best solved by a regex, but I’m not very familiar with regex. I have been toying with regexlib to see if I can figure out a regex on my own… here is what I have so far, although I imagine I’m pretty far off.
string strRegex = @"<input type=""radio"" [\s]*=[\s]*""?[^\w_]*""?[^>]*>";
RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"<input type=""radio"" name=""eq_9"" id=""eq_9_2"" onclick=""nextStepID_load(this);"" value=""Installer."" title=""912"" /><label for=""eq_9_2"">Installer</label> <br />";
string strReplace = "<button type="button"></button>";
return myRegex.Replace(strTargetString, strReplace);
I’m afraid that is not a good sign, since if you are not very familiar with regexes, then it is rather unlikely that anything is best solved by a regex. 🙁
There isn’t enough description of the problem to know for sure whether even a regex wizard could quickly craft a solution using regexes. I am pretty sure you need to do more than merely exchange one fixed string for another, because if you did, you’d’ve done that already. So parts of it must be parameterized. I just don’t know which.
Not counting whitespace, would you say that your problem is one of transforming input of this form:
into output of this form:
As you see, I’ve parameterized X, Y, Z, and N. Here are questions I have for you:
Would you say my parameterization of your problem describes it accurately?
Does this validate under some particular DTD, and if so, which one?
Are the attributes always none other than those?
Are the attributes that occur always in that precise order?
How many such things do you have to do?
Does this occur in plain HTML, or is it actually hidden in some Javascript?
Do you know whether there are any
<script>or<style>elements, or<!-- ... -->comments that contain things that look just what you are looking for?Do you know whether there are any such elements intervening in the middle of the thing you are looking for?
Are all the attributes of the form
NAME="VALUE"with only double quotes around the value, never single quotes or omitted altogether?Is the casing of the identifiers always in lower case?
Are they all in one file?
Is there some reason that your output sample lost some of its non-significant whitespace?
It is questions like these that show why the problem is almost certainly much more complicated than it appears — which begs a couple of final questions:
Have you ever used an HTML parsing class before?
Would you like to learn how?