匹配&但不匹配 的正则写法

本文介绍了一种特殊的正则表达式匹配方法,即如何正确地匹配&但要排除某些特定情况,例如不希望匹配到紧跟在&后面的特定字符。这种技巧对于处理HTML转义字符等场景非常有用。
匹配&但不匹配 怎么写?
&[^(nbsp;)]
这样其实是匹配&加一个字符,比如&1、&a,正确的写法为
&(?!nbsp;)

这样不会占一个字符。它的意思为不匹配 中的&。
那么匹配&但不匹配 或者<的写法为
&(?!nbsp;|lt;)
&lt;table align=&quot;center&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tableForm&quot; style=&quot;table-layout: fixed;&quot;&gt; &lt;colgroup&gt; &lt;col width=&quot;80&quot; /&gt; &lt;col /&gt;&lt;!--hide4phone.start--&gt; &lt;col width=&quot;80&quot; /&gt; &lt;col width=&quot;380&quot; /&gt;&lt;!--hide4phone.end--&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td style=&quot;text-align: right;&quot;&gt;&nbsp;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;*&lt;/span&gt;Subject:&lt;/td&gt; &lt;td dbf.type=&quot;required&quot; id=&quot;dbf.subject&quot;&gt;&nbsp;&lt;/td&gt; &lt;!--show4phone.start&gt;&lt;/tr&gt;&lt;tr&gt;&lt;show4phone.end--&gt; &lt;td style=&quot;text-align: right;&quot;&gt;&nbsp;Status:&lt;/td&gt; &lt;td&gt;&lt;span id=&quot;mapping.dbf.procXSource&quot;&gt;&nbsp;&lt;/span&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Responsor: &lt;span id=&quot;mapping.dbf.responsorSource&quot;&gt;&nbsp;&lt;/span&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Participants: &lt;span id=&quot;mapping.dbf.participantsSource&quot;&gt;&nbsp;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;div&gt;&nbsp;&lt;/div&gt; &lt;div style=&quot;text-align: center;&quot;&gt; &lt;h1&gt;&lt;img src=&quot;../common/logo.png&quot; /&gt; [Form Name]&lt;/h1&gt; &lt;/div&gt; &lt;div&gt;[Design form here, based on the template below, or customized by yourself after the template removed]&lt;/div&gt; &lt;table align=&quot;center&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tableListBorder&quot; style=&quot;table-layout: fixed;&quot;&gt; &lt;colgroup&gt; &lt;col width=&quot;130&quot; /&gt; &lt;col /&gt;&lt;!--hide4phone.start--&gt; &lt;col width=&quot;130&quot; /&gt; &lt;col width=&quot;330&quot; /&gt;&lt;!--hide4phone.end--&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td colspan=&quot;4&quot; colspan4phone=&quot;2&quot; dbf.source=&quot;&quot; dbf.type=&quot;&quot; style=&quot;background-color: lightyellow;&quot;&gt;&nbsp;&lt;strong&gt;[Brief Information]&lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center; width: 147px;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;*&lt;/span&gt;&nbsp;username&lt;/td&gt; &lt;td id=&quot;username&quot; style=&quot;width: 210px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;!--show4phone.start--&gt; &lt;/tr&gt; &lt;tr&gt;&lt;!--show4phone.end--&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center; width: 63px;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;*&lt;/span&gt;&nbsp;id&lt;/td&gt; &lt;td id=&quot;ID&quot; style=&quot;width: 254px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;项目名称&lt;/td&gt; &lt;td id=&quot;项目名称&quot; style=&quot;width: 210px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;装置名称&lt;/td&gt; &lt;td id=&quot;装置名称&quot; style=&quot;width: 254px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;Customer Name&lt;/td&gt; &lt;td id=&quot;CustomerName&quot; style=&quot;width: 210px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;E-NO/序列号&lt;/td&gt; &lt;td id=&quot;ENO号&quot; style=&quot;width: 254px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;产品描述&lt;/td&gt; &lt;td id=&quot;产品描述&quot; style=&quot;width: 210px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;Remark&lt;/td&gt; &lt;td id=&quot;Remark&quot; style=&quot;width: 254px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&quot;fieldLabel&quot; style=&quot;text-align: center;&quot;&gt;Subject&lt;/td&gt; &lt;td id=&quot;subjectid&quot; style=&quot;width: 210px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;td class=&quot;fieldLabel&quot; dbf.source=&quot;&quot; dbf.type=&quot;&quot; id=&quot;&quot; style=&quot;text-align: center;&quot;&gt;&nbsp;&lt;/td&gt; &lt;td dbf.source=&quot;&quot; dbf.type=&quot;&quot; id=&quot;&quot; style=&quot;width: 254px;&quot;&gt;&nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&quot;4&quot; colspan4phone=&quot;2&quot; dbf.source=&quot;&quot; dbf.type=&quot;&quot; style=&quot;background-color: lightyellow;&quot;&gt;&lt;strong&gt;&nbsp;&lt;/strong&gt;&lt;strong&gt;[Detailed Information]&lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;div style=&quot;text-align: center; margin: 20px 0;&quot;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &lt;input id=&quot;idslist&quot; name=&quot;idslist&quot; type=&quot;hidden&quot; /&gt; &nbsp;&lt;input id=&quot;sql内容&quot; name=&quot;sql内容&quot; type=&quot;hidden&quot; /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;strong&gt; &lt;input id=&quot;check&quot; name=&quot;check&quot; onclick=&quot;clickData()&quot; type=&quot;button&quot; value=&quot;check记录&quot; /&gt; &lt;/strong&gt; &lt;strong&gt; &lt;input id=&quot;check2&quot; name=&quot;check2&quot; onclick=&quot;clickData2()&quot; type=&quot;button&quot; value=&quot;下载结果&quot; /&gt; &lt;/strong&gt;&lt;/div&gt; &lt;!-- ==================== 移动端表格占位(暂使用)==================== --&gt;&lt;!-- ==================== 查询结果表格容器 ==================== --&gt; &lt;div id=&quot;resultTableContainer&quot; style=&quot;margin-top: 20px; padding: 0 10px; min-height: 20px;&quot;&gt;&lt;!-- 动态表格将插入到这里 --&gt;&lt;/div&gt; &lt;div class=&quot;slide4phone2&quot; id=&quot;reptable&quot; style=&quot;display:none;&quot;&gt; &lt;table align=&quot;center&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tableListBorder2&quot; style=&quot;width: 1300px;&quot;&gt; &lt;/table&gt; &lt;/div&gt; &lt;!-- ==================== 核心脚本:查询与渲染 ==================== --&gt;&lt;script language=&quot;javascript&quot;&gt; function clickData() { // 防止重复点击 if (window.__isQuerying) { console.warn(&quot;请勿频繁点击!&quot;); return; } window.__isQuerying = true; console.log(&#39;=== 开始执行 clickData ===&#39;); try { // 获取所有输入字段 var projectName = $(&quot;项目名称&quot;).value ? $(&quot;项目名称&quot;).value().trim() : &quot;&quot;; var deviceName = $(&quot;装置名称&quot;).value ? $(&quot;装置名称&quot;).value().trim() : &quot;&quot;; var customerName = $(&quot;CustomerName&quot;).value ? $(&quot;CustomerName&quot;).value().trim() : &quot;&quot;; var enonumber = $(&quot;ENO号&quot;).value ? $(&quot;ENO号&quot;).value().trim() : &quot;&quot;; var remark = $(&quot;Remark&quot;).value ? $(&quot;Remark&quot;).value().trim() : &quot;&quot;; var productDesc = $(&quot;产品描述&quot;).value ? $(&quot;产品描述&quot;).value().trim() : &quot;&quot;; var formsubject = $(&quot;subjectid&quot;).value ? $(&quot;subjectid&quot;).value().trim() : &quot;&quot;; /** * 转义 SQL 字符串中的单引号(&#39; &rarr; &#39;&#39;) */ function escapeSql(str) { if (typeof str !== &#39;string&#39; &amp;&amp; typeof str !== &#39;number&#39;) return &#39;&#39;; return String(str).replace(/&#39;/g, &quot;&#39;&#39;&quot;); } // 构建模糊匹配参数 var pName = &#39;%&#39; + escapeSql(projectName) + &#39;%&#39;; var dName = &#39;%&#39; + escapeSql(deviceName) + &#39;%&#39;; var cName = &#39;%&#39; + escapeSql(customerName) + &#39;%&#39;; var enoNum = &#39;%&#39; + escapeSql(enonumber) + &#39;%&#39;; var rmk = &#39;%&#39; + escapeSql(remark) + &#39;%&#39;; var prodDesc = &#39;%&#39; + escapeSql(productDesc) + &#39;%&#39;; var subj = &#39;%&#39; + escapeSql(formsubject) + &#39;%&#39;; var subj2 = &#39;%-&#39; + escapeSql(formsubject) + &#39;%&#39;; // 拼接 SQL 查询语句 var sql = `SELECT Subject, Status, 申请人, 申请日期, 产品类别, 其他, type, 内勤, 系统, customer, ProjectName, DeviceName, 新产品, ENO序列号, 目标价, 数量, 产地, 含税报价, HandlerRemark, Remark, 描述 FROM X_BPM_DWH_819_InquiryLines WHERE ISNULL(ProjectName, &#39;&#39;) LIKE &#39;${pName}&#39; AND ISNULL(DeviceName, &#39;&#39;) LIKE &#39;${dName}&#39; AND ISNULL(customer, &#39;&#39;) LIKE &#39;${cName}&#39; AND ISNULL([ENO序列号], &#39;&#39;) LIKE &#39;${enoNum}&#39; AND ISNULL(Remark, &#39;&#39;) LIKE &#39;${rmk}&#39; AND ISNULL(描述, &#39;&#39;) LIKE &#39;${prodDesc}&#39; AND (ISNULL(Subject, &#39;&#39;) LIKE &#39;${subj}&#39; OR ISNULL(Subject, &#39;&#39;) LIKE &#39;${subj2}&#39;) `; console.log(&quot;生成的SQL:&quot;, sql); // 调用后端服务获取数据 var rawResult = service(&quot;common.js&quot;, &quot;getDbsRecords&quot;, sql, &quot;array&quot;); // 解析结果(支持各种脏数据格式) let arr = parseServiceResult(rawResult); // 渲染到页面 renderTable(arr); } catch (e) { console.error(&quot;【查询失败】&quot;, e); alert(&quot;查询出错:&quot; + (e.message || e)); var container = document.getElementById(&quot;resultTableContainer&quot;); if (container) { container.innerHTML = ` &lt;div style=&quot;color:red;text-align:center;padding:10px;font-family:Arial;&quot;&gt; &lt;strong&gt;❌ 查询失败&lt;/strong&gt;&lt;br/&gt; ${e.message || e} &lt;/div&gt; `; } } finally { window.__isQuerying = false; } } /** * 安全解析 service 返回的结果 * 支持:合法JSON / 非法JSON / JS数组字面量 / 字符串化数组 / null等 */ function parseServiceResult(rawResult) { console.log(&quot;【原始返回类型】&quot;, typeof rawResult); console.log(&quot;【原始内容】&quot;, rawResult); // 情况1:已经是数组 if (Array.isArray(rawResult)) { console.log(&quot;✅ 已识别为原生数组&quot;); return rawResult; } // 情况2:null/undefined/false if (rawResult == null || rawResult === &#39;null&#39; || rawResult === &#39;undefined&#39;) { console.warn(&quot;⚠️ 返回为空值&quot;); return []; } // 转为字符串处理 let str = String(rawResult).trim(); if (!str) { console.warn(&quot;⚠️ 返回空字符串&quot;); return []; } // 情况3:可能是HTML错误页(常见于服务异常) if (str.startsWith(&#39;&lt;&#39;) || str.includes(&#39;&lt;html&#39;) || str.toLowerCase().includes(&#39;error&#39;) || str.includes(&#39;Exception&#39;)) { console.error(&quot;🔴 返回了HTML错误页!&quot;, str.substring(0, 200)); throw new Error(&#39;服务端错误,请检查SQL语法或联系管理员&#39;); } /** * 数据清洗函数:修复各种非法字符 */ function clean(str) { return str .replace(/\\\\/g, &#39;\\&#39;) // 修复多个反斜杠 .replace(/&quot;/g, &#39;\\&quot;&#39;) // 先转义双引号 .replace(/&#39;/g, &#39;&quot;&#39;) // 单引号 &rarr; 双引号 .replace(/\\&quot;/g, &#39;&quot;&#39;) // 还原正常引号 .replace(/\\n/g, &#39;\\n&#39;) // 显式换行 .replace(/\\r/g, &#39;\\r&#39;) .replace(/\\t/g, &#39;\\t&#39;) .replace(/\n/g, &#39;\\n&#39;) .replace(/\r/g, &#39;\\r&#39;) .replace(/\t/g, &#39;\\t&#39;) .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, &#39;&#39;) // 移除控制字符 .replace(/^&quot;(\[.*\])&quot;$/, &#39;$1&#39;) // 去掉 &quot;[...]&quot; 外层引号 .replace(/^&quot;(\{.*\})&quot;$/, &#39;$1&#39;) .replace(/,\s*}/g, &#39;}&#39;) // 去除对象尾部逗号 .replace(/,\s*]/g, &#39;]&#39;); // 去除数组尾部逗号 } // 尝试多种解析方式 const parsers = [ // 方式1:直接JSON.parse () =&gt; JSON.parse(str), // 方式2:清洗后再解析 () =&gt; JSON.parse(clean(str)), // 方式3:作为JS表达式执行(仅限可信环境) () =&gt; new Function(`return (${str});`)(), // 方式4:进一步清理后尝试 () =&gt; JSON.parse(clean(str).replace(/&quot;&quot;/g, &#39;&quot;&#39;).replace(/&quot;\s+:/g, &#39;&quot;:&#39;).replace(/:\s+&quot;/g, &#39;:&quot;&#39;)) ]; for (let i = 0; i &lt; parsers.length; i++) { try { const result = parsers[i](); if (Array.isArray(result)) { console.log(`✅ 第 ${i+1} 种方式解析成功`); return result; } // 兼容 { data: [...] } 结构 if (result &amp;&amp; Array.isArray(result.data)) return result.data; if (result &amp;&amp; Array.isArray(result.rows)) return result.rows; } catch (e) { console.warn(`📌 方式 ${i+1} 解析失败`, e.message); } } // 情况5:手动解析简单二维数组字符串 [[...],[...]] if (/^\[\s*$$[^$$]*$\s*(,\s*$$[^$$]*$)*\s*\]$/.test(str)) { try { console.log(&quot;🔍 检测到疑似二维数组字符串,尝试手动解析&quot;); const rows = str.match(/$[^$$]*$/g) || []; return rows.map(row =&gt; { return row.replace(/^\[/, &#39;&#39;).replace(/\]$/, &#39;&#39;) .split(&#39;,&#39;) .map(field =&gt; field.trim().replace(/^&quot;(.*)&quot;$/, &#39;$1&#39;).replace(/^&#39;(.*)&#39;$/, &#39;$1&#39;)); }).filter(r =&gt; r.length &gt; 0); } catch (e) { console.error(&quot;手动解析失败&quot;, e); } } // 所有方式都失败 console.error(&quot;🔴 所有解析方式均失败&quot;, { raw: rawResult, cleaned: clean(str) }); throw new Error(&#39;返回数据格式异常,无法解析。请检查SQL是否正确或联系开发人员。&#39;); } /** * 渲染查询结果为表格 */ function renderTable(arr) { var container = document.getElementById(&quot;resultTableContainer&quot;); if (!container) return; // 清空容器 container.innerHTML = &#39;&#39;; if (!arr || arr.length === 0) { container.innerHTML = ` &lt;p style=&quot;text-align:center;color:#999;font-style:italic;padding:20px;&quot;&gt; 📭 未找到匹配的数据。 &lt;/p&gt; `; return; } // 创建表格 var table = document.createElement(&#39;table&#39;); table.className = &#39;tableListBorder&#39;; table.style.width = &#39;100%&#39;; table.style.tableLayout = &#39;fixed&#39;; table.setAttribute(&#39;border&#39;, &#39;1&#39;); table.setAttribute(&#39;cellpadding&#39;, &#39;6&#39;); table.setAttribute(&#39;cellspacing&#39;, &#39;0&#39;); table.style.fontFamily = &#39;Arial, sans-serif&#39;; table.style.fontSize = &#39;13px&#39;; // 表头 var thead = document.createElement(&#39;thead&#39;); var headerRow = document.createElement(&#39;tr&#39;); var headers = [ &quot;Subject&quot;, &quot;Status&quot;, &quot;申请人&quot;, &quot;申请日期&quot;, &quot;产品类别&quot;, &quot;其他&quot;, &quot;类型&quot;, &quot;内勤&quot;, &quot;系统&quot;, &quot;客户&quot;, &quot;项目名称&quot;, &quot;装置名称&quot;, &quot;新产品&quot;, &quot;E-NO/序列号&quot;, &quot;目标价&quot;, &quot;数量&quot;, &quot;产地&quot;, &quot;含税报价&quot;, &quot;处理备注&quot;, &quot;备注&quot;, &quot;描述&quot; ]; headers.forEach(function(text) { var th = document.createElement(&#39;th&#39;); th.style.backgroundColor = &#39;#f5f5f5&#39;; th.style.textAlign = &#39;center&#39;; th.style.padding = &#39;10px&#39;; th.style.whiteSpace = &#39;nowrap&#39;; th.style.fontWeight = &#39;bold&#39;; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // 表体 var tbody = document.createElement(&#39;tbody&#39;); arr.forEach(function(row, index) { if (!row || !Array.isArray(row) &amp;&amp; typeof row !== &#39;object&#39;) { console.warn(`第 ${index} 行数据异常`, row); return; } var tr = document.createElement(&#39;tr&#39;); Object.values(row).forEach(function(cell) { var td = document.createElement(&#39;td&#39;); td.style.padding = &#39;8px&#39;; td.style.borderTop = &#39;1px solid #eee&#39;; td.style.wordBreak = &#39;break-word&#39;; td.style.maxWidth = &#39;200px&#39;; td.textContent = cell == null ? &#39;&#39; : String(cell).trim(); tr.appendChild(td); }); tbody.appendChild(tr); }); table.appendChild(tbody); container.appendChild(table); console.log(`✅ 成功渲染 ${arr.length} 行数据`); } &lt;/script&gt; &lt;div id=&quot;resultTableContainer&quot; style=&quot;margin-top: 20px; overflow-x: auto;&quot;&gt;&nbsp;&lt;/div&gt; [ENO序列号] 文字可以模糊匹配 但是纯数字 888888只能匹配到8
最新发布
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值