求正则表达式(可追加100分)

sunxingtao 2011-10-12 05:30:26
我想把代码中的align=left|right|center等删掉,不删除其它标签中的align

<table name=mytable align=left>
<tr>
<td>
</td>
</tr>
</table>
<div name=tt align=right ></div>
<table width=80px align = left>
<tr>
<td>
</td>
</tr>
</table>

要考虑=号两个有空格的情况
要考虑right两边有单双引号的情况
期待完成的代码为

<table name=mytable >
<tr>
<td>
</td>
</tr>
</table>
<div name=tt align=right ></div>
<table width=80px >
<tr>
<td>
</td>
</tr>
</table>
...全文
112 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2011-10-13
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<script>

var html ='<table name=mytable align=\'left\'>\
<tr>\
<td>\
</td>\
</tr>\
</table>\
<div name=tt align=right ></div>\
<table width=80px align = "left">\
<tr>\
<td>\
</td>\
</tr>\
</table>\
';
var reg = /(<table[^>]*?)align\s*=\s*(['\"]?)(?:right|left|center)\2([^>]*?>)/gi;
html = html.replace(reg, "$1$3");
alert(html);
</script>
</body>
</html>
我的积分呢 2011-10-13
  • 打赏
  • 举报
回复
哎。。问题解决了还不结贴。。。接分
q107770540 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sunxingtao 的回复:]

要考虑right两边有单双引号的情况
如align='left' 和 align="right"
大侠 这个还不行
[/Quote]
4楼的已经考虑到单双引号了
sunxingtao 2011-10-13
  • 打赏
  • 举报
回复
再多问句 怎么追加分?
sunxingtao 2011-10-13
  • 打赏
  • 举报
回复
要考虑right两边有单双引号的情况
如align='left' 和 align="right"
大侠 这个还不行
BLUE_LG 2011-10-12
  • 打赏
  • 举报
回复
呵呵,已经解决了,就早点给人结贴吧,看着难受。。。
yhtapmys 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 soonfei 的回复:]
引用 5 楼 soonfei 的回复:
JScript code
var str = "<table name=mytable align=left><tr><td></td></tr></table><div name=tt align=right ></div><table width=80px align = left><tr><td></td></tr></table>";
var ……
[/Quote]

哈哈 正所谓英雄所见略同...
诺维斯基 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 soonfei 的回复:]
JScript code
var str = "<table name=mytable align=left><tr><td></td></tr></table><div name=tt align=right ></div><table width=80px align = left><tr><td></td></tr></table>";
var reg = /(<……
[/Quote]
我用了30分钟做出来。
怎么看都是抄袭4楼的。
诺维斯基 2011-10-12
  • 打赏
  • 举报
回复
var str = "<table name=mytable align=left><tr><td></td></tr></table><div name=tt align=right ></div><table width=80px align = left><tr><td></td></tr></table>";
var reg = /(<table[^>]*?)align\s*=\s*([\'\"]?)(?:left|right|center)\2[^>]*(>)/g;
var newstr = str.replace(reg, "$1>");
alert(newstr);
q107770540 2011-10-12
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<script>

var html ='<table name=mytable align=left>\
<tr>\
<td>\
</td>\
</tr>\
</table>\
<div name=tt align=right ></div>\
<table width=80px align = left>\
<tr>\
<td>\
</td>\
</tr>\
</table>\
';
var reg = /(<table[^>]*?)align\s*=\s*([\'\"]?)(?:right|left|center)\2([^>]*?>)/gi;
html = html.replace(reg, "$1$3");
alert(html);
</script>
</body>
</html>

q107770540 2011-10-12
  • 打赏
  • 举报
回复
/(<table[^>]*?)align\s*=\s*(['"]?)(?:right|left|center)\2([^>]*?>)/gi
Y叔输得起 2011-10-12
  • 打赏
  • 举报
回复
document.getElement("table").removeAttribute("align")
Y叔输得起 2011-10-12
  • 打赏
  • 举报
回复
$("table").removeAttr("align");

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧