2012-11-18 158 views
0

我有這樣的HTML代碼:替換HTML表單元素?

<HTML> 
<HEAD> 
<TITLE></TITLE> 

</HEAD> 

<BODY> 

<form action="http://www.aweber.com/scripts/addlead.pl" method="post"> 
<input id="email" class="text" type="text" size="20" value="Enter your best email" name="email" onblur="if (this.value == '') {this.value = 'Enter your best email';}" onfocus="if (this.value == 'Enter your best email') {this.value = '';}" /> 
<input type="hidden" name="meta_web_form_id" value=""> 
<input type="hidden" name="meta_split_id" value=""> 
<input type="hidden" name="listname" value=""> 
<input type="hidden" name="redirect" value=""> 
<input type="hidden" name="meta_adtracking" value=""> 
<input type="hidden" name="meta_message" value="1"> 
<input type="hidden" name="meta_required" value="email"> 
<input type="hidden" name="meta_tooltip" value=""> 
<div style="padding-top:5px;"> 
<center><input class="button1" id="submit" type="image" src="red_getwaitinglist.png" name="submit" value="Get Instant Access"/></center> 
</div> 
</form> 

</BODY> 
</HTML> 

和我使用這個功能使用正則表達式來替換元素:

Function StripTags(ByVal html As String, ByVal replace As String) As String 
    Return Regex.Replace(html, "<form.+?</form>", replace) 
End Function 

,但它似乎並沒有工作。它返回相同的代碼(沒有替換)。

我不太瞭解正則表達式,所以我假定RegEx語法可能是錯誤的。 有人可以幫助我,或讓我知道我錯在哪裏?

+1

您應該使用HtmlAgilityPack。 – SLaks

+1

您應該使用HtmlAgilityPack,因爲在HTML中使用正則表達式存在非常嚴重的問題。只是不要那樣做。 –

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

附加RegexOption Singleline。沒有這個.將不匹配換行符。

Return Regex.Replace(html, "<form.+?</form>", replace, RegexOptions.Singleline) 

請參閱RegexOptions Enumeration瞭解更多詳情。

+0

哇這個似乎奇蹟般地工作,謝謝奧拉夫! –