<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>id检重</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="zh-CN" /> </head> <body> <div id="abc"> <div class="abc"> <span class="item1"></span> <span class="item2"></span> <span class="item3"></span> <span class="item4"></span> id="abc" </div> <!-- <div id="abc"></div> --> </div> <script type="text/javascript"> // id="abc" /** * * @author Zjmainstay * @website http://zjmainstay.cn * @copyright GPL * @version 1.0 * @year 2014 * */ //删除注释 //删除页内js内容(script标签也可能有id) //限定标签id var m; if(!!(m = document.getElementsByTagName("body")[0].innerHTML .replace(/<!--[\s\S]*?-->/gi, '') .replace(/(<script[^>]*?>)[\s\S]*?(<\/script>)/gi, '$1$2') .match(/<[^>]*?\s(id="[^"]*?")[^>]*?>(?=[\s\S]*?<[^>]*?\1[^>]*?>)/gi))) { console.log('有重复id'); console.log(m); } else { console.log('没有重复id'); } </script> </body> </html>
未经同意禁止转载!
转载请附带本文原文地址:js正则表达式实现id重复检测功能,首发自 Zjmainstay学习笔记