问题描述
求一js正则表达式把页面中的p标签全部换成div
解决方案
<!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></title></head><body><p class="p1">asdfaspf</p><p class="p2">asdpfasf</p><script type="text/javascript">var body = document.getElementsByTagName('body')[0];body.innerHTML = body.innerHTML.replace(/<p([^<>]*)>([^<>]*)</p>/gi, '<div$1>$2</div>'); </script></body></html>
解决方案二:
为什么body.innerHTML复制进去以后会变成bodybody.innerHTML?不给力啊,LZ自己改回来吧。。
解决方案三:
<!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></title></head><body><p class="p1">asdfaspf</p><p class="p2">asdpfasf</p><script type="text/javascript">var body = document.getElementsByTagName('body')[0];body.innerHTML = body.innerHTML.replace(/<p([S ]*?>[S ]*?)</p>/gi, '<div$1</div>'); </script></body></html>
解决方案四:
修正了bug,这个是最终版<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><script type="text/javascript">window.onload = function() {document.body.innerHTML = document.body.innerHTML.replace(/<([/]?)(p)((:?s*)(:?[^>]*)(:?s*))>/g, '<$1div$3>'); };</script></head><body><span>xixi</span><p class="aaa" style="text-align=center;">ha<span>xixi</span>ha</p><div><p>hehe</p></div></body></html>
解决方案五:
写的不是很完善,基本上满足功能。<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">window.onload = function() {document.body.innerHTML = document.body.innerHTML.replace(/<[/]?(p)(:?s+(:?class|style)=(['"])[^'"]*['"])*>/g, function(m, m1) {return m.replace('p', 'div');}); };</script></head><body><span>xixi</span><p class="aaa" style="text-align=center;">haha</p><p>hehe</p></body></html>