求一正则表达式,替换表格中宽度属性
用户上传的新闻,有些内容中的表格定义了宽度,显示的时候会把页面撑开,有没有办法把<table><tr><td>中宽度属性去掉。不影响其它HTML标签中的宽度属性。--------------------编程问答-------------------- 那为高手给个答案 --------------------编程问答-------------------- 没有人知道嘛 --------------------编程问答--------------------
--------------------编程问答-------------------- 加个参数 RegexOptions.IgnoreCase --------------------编程问答-------------------- 加个参数 RegexOptions.IgnoreCase
string str = "*******";
str = Regex.Replace(str, @"(?<=<[^>]*?)width(=|:)[""']?[^;\s]*[""']?;*(?=[^>]*>)");
//支持<table style="width:10px;height:100px;">
//支持<td width="100px">
//支持单引号双引号
//有一个小缺陷 style="width:10px;" 替换完后会变成 style="" 显示的话应该不太影像
--------------------编程问答-------------------- 这个会不会把所有标签里面的宽度都替换掉
string str = "*******";
str = Regex.Replace(str, @"(?<=<[^>]*?)width(=|:)[""']?[^;\s]*[""']?;*(?=[^>]*>)", "", RegexOptions.IgnoreCase);
//没写完发出去了- -!
我只去掉<table><tr><td>里的宽度去掉哟 --------------------编程问答--------------------
--------------------编程问答--------------------
"(?<=<(table|td|tr)[^>]*?)width(=|:)[""']?[^;\s]*[""']?;*(?=[^>]*>)"
--------------------编程问答-------------------- 辛苦一晚上做出来的,功能没问题,实现方式可能不是最佳,width:100px"用正则表达区分还没实现好
"(?<=<(table|td|tr)[^>]*?)width(=|:)[""']?[^;\s>]*[""']?;*(?=[^>]*>)"
//有点小问题 再更新一下
--------------------编程问答-------------------- 那匹配 width="571" width=139 WIDTH: 160px; 的正则表达试怎么写 --------------------编程问答-------------------- 哪为帮我写一个 --------------------编程问答-------------------- 没有人知道吗 --------------------编程问答--------------------
public partial class Form1 : Form
{
Regex Rg = new Regex("width=\".*?\"|(width:.+?)[;\"]{1}");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "//test.txt", Encoding.Default);
string content = sr.ReadToEnd(); //用的时候在把content等于用户输入的内容
Regex rg = new Regex("<t.*?(width=\".*?\"|[;\"]{1}width:.+?[;\"]{1})?.*?>");
MatchEvaluator myEvaluator = new MatchEvaluator(Replace);
richTextBox1.Text = rg.Replace(content, myEvaluator);
}
private string Replace(Match mc)
{
MatchEvaluator myEvaluator = new MatchEvaluator(Replace1);
return Rg.Replace(mc.Value, myEvaluator);
}
private string Replace1(Match mc)
{
if (mc.Value.Contains("\"")) return Rg.Replace(mc.Value, "\"");
return Rg.Replace(mc.Value, "");
}
}
--------------------编程问答--------------------
@"(?i)width[=:]\s*"?[^";\s]+[";]?"
@"(?i)width[=:]\s*['""]?[^""';\s]+[""';]?"
//匹配 width="571" width=139 WIDTH: 160px; width='139'
--------------------编程问答--------------------
补充:.NET技术 , ASP.NET