![](/img/trans.png)
[英]Regex Replace all found occurrences if the word not matches with the given prefix
[英]Regex replace doesn't replace all occurrences
我使用下面的正則表達式替換兩個單詞之間的文本。 它可以工作,只是它跳過了其中一些。 下面粘貼的是一個示例。
var EditedHtml = Regex.Replace(htmlText, @"<script(.*?)</script>", "");
htmlText:
<head>
<script src=" https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src=" https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
<script src="/AspellWeb/v2/js/dragiframe.js" type="text/javascript"></script>
<script type="text/javascript">
var applicationName = '/';
FullPath = (applicationName.length > 1) ? 'http://localhost:65355' + applicationName : 'http://localhost:65355';
//FullPath = 'http://localhost:65355';
GetPath = function (url) {
return FullPath + url;
}
</script>
<script type="text/javascript" src="../../Scripts/stats.js?"></script>
</head>
<body>
.......
<script type="text/javascript">
function loadAndInit() {
$(".dvloading").hide();
if ($.browser.mozilla) {
if (location.pathname == "/Stats/Reports") { // This is for local env.
$("#prntCss").attr("href", "../../../Content/SitePrint_FF.css");
}
else { // This is for DEV/QA/STAGE/PROD env.
$("#prntCss").attr("href", "../../Content/SitePrint_FF.css");
}
}
}
</script>
</body>
EditedHtml:
<head>
<script type="text/javascript">
var applicationName = '/';
FullPath = (applicationName.length > 1) ? 'http://localhost:65355' + applicationName : 'http://localhost:65355';
//FullPath = 'http://localhost:65355';
GetPath = function (url) {
return FullPath + url;
}
</script>
</head>
<body>
.......
<script type="text/javascript">
function loadAndInit() {
$(".dvloading").hide();
if ($.browser.mozilla) {
if (location.pathname == "/Stats/Reports") { // This is for local env.
$("#prntCss").attr("href", "../../../Content/SitePrint_FF.css");
}
else { // This is for DEV/QA/STAGE/PROD env.
$("#prntCss").attr("href", "../../Content/SitePrint_FF.css");
}
}
}
</script>
</body>
為什么使用Regex解析html。 看到這個
使用像HtmlAgilityPack這樣的真正的html解析器會容易得多
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(filename); //or doc.LoadHtml(HtmlString)
doc.DocumentNode.Descendants()
.Where(n => n.Name == "script").ToList()
.ForEach(s => s.Remove());
StringWriter wr = new StringWriter();
doc.Save(wr);
var newhtml = wr.ToString();
在單行模式下嘗試:
var EditedHtml = Regex.Replace(
htmlText, @"<script(.*?)</script>", "",
RegexOptions.Singleline);
文檔報價:
指定單行模式。 更改點(。)的含義,使其匹配每個字符(而不是\\ n以外的每個字符)。
嘗試
var EditedHtml = Regex.Replace(
htmlText, @"<script(.*?)</script>", "", RegexOptions.Singleline
);
使用單行模式,這樣.
匹配任何字符, 包括換行符。
嘗試這個:
//(.|\r\n)*: matches every character and/or newline zero or more times
//(.|\r\n)*?: as few times as possible == > you get rid of <script> tags and of their content but you keep the rest of your html
var EditedHtml = Regex.Replace(htmlText, @"<script (.|\r\n)*?</script>", "");
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.