Click here to Skip to main content
15,888,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, call me inept but I've tried all sorts of things to solve this using regular expressions and standard replace functions. I really cannot work out how to do it.

My problem is that jQuery's html() function returns innerHtml, but for input tags, it misses off the closing slash. e.g. in html the tag would be: <input type="hidden" value="Hello world!" /> but jQWuery returns <input type="hidden" value="Hello world!" > - without the closing slash! I figured rather than trying to fix the jQuery, I would simply add in the ending slash server side when the html was received (through an AJAX request). However, my problem is how to replace <input *** > with <input *** /> without losing the content between the opening < and closing >?.

Thanks very much,

Ed
Posted

Try:
public static Regex regex = new Regex(
      @"\<input[^\>]*",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

// This is the replacement string
public static string regexReplace = 
      "$& /";

// Replace the matched text in the InputText using the replacement pattern
 string result = regex.Replace(InputText,regexReplace);


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions. It generated the code above...
 
Share this answer
 
Comments
Ed Nutting 12-Jul-11 12:21pm    
Wow thanks! That seems to work just fine :) Ill use that and repeat similar code for img tags (and any others I happen to find :S). Thanks very much :D
why don't you just replace ">" with "/>" but only if a) the string starts with "<input" and b) doesn't end with "/>"?
 
Share this answer
 
Comments
Ed Nutting 12-Jul-11 12:16pm    
Doesn't work, I have a mass of html, mixture of divs, inputs, img tags an the ones where the end tag is also that start tag (e.g. img, input tags) jQuery misses off the slashes. I need to be able to mass replace. I'm starting to wonder if regex will do what I want or whether I need to write a recursive loop? :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900