替换为 [英] Replacing with

查看:65
本文介绍了替换为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

称我无能为力,但我已经尝试过各种方法来使用正则表达式和标准替换函数来解决此问题.我真的不知道该怎么做.

我的问题是jQuery的html()函数返回innerHtml,但是对于input标记,它没有使用斜杠.例如在html中,标记为:<input type="hidden" value="Hello world!" />,但jQWuery返回<input type="hidden" value="Hello world!" >-不带斜杠!我想而不是尝试修复jQuery,我只是在接收到html时(通过AJAX请求)在结尾的斜杠服务器端添加即可.但是,我的问题是如何替换< input ***>与< input ***/>而不损失开头之间的内容<并关闭>?.

非常感谢,

Ed

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

推荐答案

尝试:
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);
& /"; // Replace the matched text in the InputText using the replacement pattern string result = regex.Replace(InputText,regexReplace);



获取 Expresso [ ^ ]-它是免费的,它检查并生成正则表达式.它生成了上面的代码...



Get a copy of Expresso [^] - it''s free, and it examines and generates Regular expressions. It generated the code above...


为什么不只替换>"用"/>"但仅当a)字符串以< input"开头且b)并非以"/>"结尾时?
why don''t you just replace ">" with "/>" but only if a) the string starts with "<input" and b) doesn''t end with "/>"?


这篇关于替换为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆