日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

.Net(C#)用正则表达式清除HTML标签(包括script和style),保留纯本文(UEdit中编写的内容上传到数据库)...

發布時間:2024/4/17 C# 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .Net(C#)用正则表达式清除HTML标签(包括script和style),保留纯本文(UEdit中编写的内容上传到数据库)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

去官網下載,本Demo用的MVC模式

下載地址:http://ueditor.baidu.com/website/download.html

加入文件夾中的結構:

引入了函數公式的圖標:

@{ViewBag.Title = "Index"; }@*配置文件*@ <script src="~/Scripts/ueditor/ueditor.config.js"></script> <script src="~/Scripts/ueditor/ueditor.all.min.js"></script> <link href="~/Scripts/ueditor/themes/iframe.css" rel="stylesheet" /> <script src="~/Scripts/ueditor/lang/zh-cn/zh-cn.js"></script>@*函數公式插件引入的js*@ <script type="text/javascript" charset="utf-8" src="~/Scripts/ueditor/kityformula-plugin/addKityFormulaDialog.js"></script> <script type="text/javascript" charset="utf-8" src="~/Scripts/ueditor/kityformula-plugin/getKfContent.js"></script> <script type="text/javascript" charset="utf-8" src="~/Scripts/ueditor/kityformula-plugin/defaultFilterFix.js"></script>@{ViewBag.Title = "UEditorDemo"; }<script type="text/javascript">var editor = new baidu.editor.ui.Editor({UEDITOR_HOME_URL: '/Scripts/ueditor/',//配置編輯器路徑iframeCssUrl: '/Scripts/ueditor/themes/iframe.css',//樣式路徑initialContent: '',//初始化編輯器內容autoHeightEnabled: true,//高度自動增長minFrameHeight: 500,//最小高度autoFloatEnabled: true,initialFrameWidth: 784,initialFrameHeight: 400});editor.render('editor'); </script></div>@using (Html.BeginForm("Index", "UEditor", FormMethod.Post)) {<div></div><div>內容</div><div> <textarea id="editor" name="editor"></textarea></div><input type="submit" value="提交" /> }<div><!--轉化圖片格式的--><button οnclick="ReplaceImage()">imagebase64替換為image</button><button οnclick="getAllHtml()">獲得整個html的內容</button><button οnclick="getContent()">獲得內容</button><button οnclick="setContent()">寫入內容</button><button οnclick="setContent(true)">追加內容</button><button οnclick="getContentTxt()">獲得純文本</button><button οnclick="getPlainTxt()">獲得帶格式的純文本</button><button οnclick="hasContent()">判斷是否有內容</button><button οnclick="setFocus()">使編輯器獲得焦點</button><button οnmοusedοwn="isFocus(event)">編輯器是否獲得焦點</button><button οnmοusedοwn="setblur(event)">編輯器失去焦點</button></div> <div><button οnclick="getText()">獲得當前選中的文本</button><button οnclick="insertHtml()">插入給定的內容</button><button id="enable" οnclick="setEnabled()">可以編輯</button><button οnclick="setDisabled()">不可編輯</button><button οnclick=" UE.getEditor('editor').setHide()">隱藏編輯器</button><button οnclick=" UE.getEditor('editor').setShow()">顯示編輯器</button><button οnclick=" UE.getEditor('editor').setHeight(300)">設置高度為300默認關閉了自動長高</button> </div><div><button οnclick="getLocalData()">獲取草稿箱內容</button><button οnclick="clearLocalData()">清空草稿箱</button> </div><div><button οnclick="createEditor()">創建編輯器</button><button οnclick="deleteEditor()">刪除編輯器</button> </div> <script type="text/javascript">//實例化編輯器//建議使用工廠方法getEditor創建和引用編輯器實例,如果在某個閉包下引用該編輯器,直接調用UE.getEditor('editor')就能拿到相關的實例var ue = UE.getEditor('editor');//將image的src從base64替換為文件名function ReplaceImage() {ue.getKfContent(function (content) { });}function isFocus(e) {alert(UE.getEditor('editor').isFocus());UE.dom.domUtils.preventDefault(e)}function setblur(e) {UE.getEditor('editor').blur();UE.dom.domUtils.preventDefault(e)}function insertHtml() {var value = prompt('插入html代碼', '');UE.getEditor('editor').execCommand('insertHtml', value)}function createEditor() {enableBtn();UE.getEditor('editor');}function getAllHtml() {alert(UE.getEditor('editor').getAllHtml())}function getContent() {var arr = [];arr.push("使用editor.getContent()方法可以獲得編輯器的內容");arr.push("內容為:");arr.push(UE.getEditor('editor').getContent());alert(arr.join("\n"));}function getPlainTxt() {var arr = [];arr.push("使用editor.getPlainTxt()方法可以獲得編輯器的帶格式的純文本內容");arr.push("內容為:");arr.push(UE.getEditor('editor').getPlainTxt());alert(arr.join('\n'))}function setContent(isAppendTo) {var arr = [];arr.push("使用editor.setContent('歡迎使用ueditor')方法可以設置編輯器的內容");UE.getEditor('editor').setContent('歡迎使用ueditor', isAppendTo);alert(arr.join("\n"));}function setDisabled() {UE.getEditor('editor').setDisabled('fullscreen');disableBtn("enable");}function setEnabled() {UE.getEditor('editor').setEnabled();enableBtn();}function getText() {//當你點擊按鈕時編輯區域已經失去了焦點,如果直接用getText將不會得到內容,所以要在選回來,然后取得內容var range = UE.getEditor('editor').selection.getRange();range.select();var txt = UE.getEditor('editor').selection.getText();alert(txt)}function getContentTxt() {var arr = [];arr.push("使用editor.getContentTxt()方法可以獲得編輯器的純文本內容");arr.push("編輯器的純文本內容為:");arr.push(UE.getEditor('editor').getContentTxt());alert(arr.join("\n"));}function hasContent() {var arr = [];arr.push("使用editor.hasContents()方法判斷編輯器里是否有內容");arr.push("判斷結果為:");arr.push(UE.getEditor('editor').hasContents());alert(arr.join("\n"));}function setFocus() {UE.getEditor('editor').focus();}function deleteEditor() {disableBtn();UE.getEditor('editor').destroy();}function disableBtn(str) {var div = document.getElementById('btns');var btns = UE.dom.domUtils.getElementsByTagName(div, "button");for (var i = 0, btn; btn = btns[i++];) {if (btn.id == str) {UE.dom.domUtils.removeAttributes(btn, ["disabled"]);} else {btn.setAttribute("disabled", "true");}}}function enableBtn() {var div = document.getElementById('btns');var btns = UE.dom.domUtils.getElementsByTagName(div, "button");for (var i = 0, btn; btn = btns[i++];) {UE.dom.domUtils.removeAttributes(btn, ["disabled"]);}}function getLocalData() {alert(UE.getEditor('editor').execCommand("getlocaldata"));}function clearLocalData() {UE.getEditor('editor').execCommand("clearlocaldata");alert("已清空草稿箱")} </script>

?

想把內容保存進去,但是有HTML標簽,正則處理的代碼:

public static string CleanHtml(string strHtml){if (string.IsNullOrEmpty(strHtml))return strHtml;//刪除腳本//strHtml = Regex.Replace(strHtml, "(+'\'+<script(.+?)+'\'+</script+'\'+>)|(+'\'+<style(.+?)+'\'+</style+'\'+>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);strHtml = Regex.Replace(strHtml, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);//刪除標簽var r = new Regex(@"</?[^>]*>", RegexOptions.IgnoreCase);Match m;for (m = r.Match(strHtml); m.Success; m = m.NextMatch()){strHtml = strHtml.Replace(m.Groups[0].ToString(), "");}return strHtml.Trim();}

?

轉載于:https://www.cnblogs.com/sunliyuan/p/8609454.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的.Net(C#)用正则表达式清除HTML标签(包括script和style),保留纯本文(UEdit中编写的内容上传到数据库)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。