日期:2014-05-16  浏览次数:20299 次

javascript如何使用正则比配有规律的html标签
要匹配的标签如下:


<input name="rptQuestions:_ctl0:txtQuestionType" type="text" value="1" id="rptQuestions__ctl0_txtQuestionType" style="width:0px;" />
<input name="rptQuestions:_ctl0:txtQuestionTypes" type="text" value="56" id="rptQuestions__ctl0_txtQuestionTypes" style="width:0px;" />

匹配整个标签,我要把字符合条件的标签删除。
其中主要以name和id中的txtQuestionType 做主要标识

改怎么样写正则表达式才可以删除name和ID中存在txtQuestionType 的整个标签


------解决方案--------------------
JScript code
  var s='<input name="rptQuestions:_ctl0:txtQuestionType" type="text" value="1" id="rptQuestions__ctl0_txtQuestionType" style="width:0px;" /><input name="rptQuestions:_ctl0:txtQuestionTypes" type="text" value="56" id="rptQuestions__ctl0_txtQuestionTypes" style="width:0px;" />'
var p=/<input\s*name=".*?(?:txtQuestionType)"[^>]*>/gi;
alert(s.replace(p,''));

------解决方案--------------------
探讨

如果用jQuery的话:
$("input[name^='rptQuestions'][id=^='rptQuestions']").remove();
“^=”配置以属性以给定字符串开头的元素