理解有误。(?!pattern)A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of ``foo'' that isn't followed by ``bar''. Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind.
\< <也需要转义吗? (?<=\<table) <table是字符串,这里的大于等于有什么意义?
在?<= 里,< 需要转义,其它情况不需要转义。(?<=pattern)A zero-width positive lookbehind assertion. For example, /(?<=\t)\w+/ matches a word following a tab, without including the tab in $&. Works only for fixed-width lookbehind.
echo preg_replace( '/(?<=\<table)\s+width="\d+"/is', '',$html);
改成这样:
$html = '<table width="20"><tr><td width="10">sdfsdf</td><td width="50"></tr></table>';
echo preg_replace( '/(?!(?<=\<table))\s+width="\d+"/is', '',$html);
<table width="100"><tr><td width="100"></td></tr></table>
<img width="200">
11111111111width="300"22222222222替换成:
<table width="100"><tr><td width=""></td></tr></table>
<img width="">
11111111111width=""22222222222
echo preg_replace( '/(?<=\S)width="\d+"/is', '',$html);
\<
<也需要转义吗?
<img是字符串,这里的大于等于有什么意义?'/(?!<table.*?)(width="\d+")/is';
这句为什么不行,即要把除table里的参数外所有的width=""字串都删除。
<也需要转义吗?
(?<=\<table)
<table是字符串,这里的大于等于有什么意义?
真是不错啊。
这个是用JS。没有用预查。。JS里没正向预查。。
<script type="text/javascript">
var s = '<xx width="100000"><xx width=\'100000\'><wc width=100><table width="100"></table><\/xx>';
var p = /(<table[^>]+>)|(<[^>]+?)\swidth=([\"']?)\d+\3([^>]*>)/gi;
var ns = s.replace(p, function (a, b, c, d, e) {
if (c) {
return c + e;
} else {
return a;
}
});
alert(ns);
</script>