()
| 1547 | testTextArea.addEventListener('input', doVisualTest); |
| 1548 | |
| 1549 | function doVisualTest() { |
| 1550 | let regexStr = regexInput.value.trim(); |
| 1551 | const testText = testTextArea.value.trim(); |
| 1552 | |
| 1553 | errorMsg.textContent = ''; |
| 1554 | |
| 1555 | if (!regexStr) { |
| 1556 | errorMsg.textContent = '请输入正则表达式'; |
| 1557 | return; |
| 1558 | } |
| 1559 | if (!testText) { |
| 1560 | errorMsg.textContent = '请输入要测试的文本'; |
| 1561 | return; |
| 1562 | } |
| 1563 | |
| 1564 | // 新增:支持 /pattern/flags 写法 |
| 1565 | let pattern = regexStr; |
| 1566 | let flags = Array.from(flagCheckboxes) |
| 1567 | .filter(cb => cb.checked) |
| 1568 | .map(cb => cb.value) |
| 1569 | .join(''); |
| 1570 | |
| 1571 | // 如果是 /pattern/flags 形式 |
| 1572 | const regSlash = /^\/(.+)\/([gimsuy]*)$/; |
| 1573 | const match = regexStr.match(regSlash); |
| 1574 | if (match) { |
| 1575 | pattern = match[1]; |
| 1576 | flags = match[2]; |
| 1577 | } |
| 1578 | |
| 1579 | try { |
| 1580 | const regex = new RegExp(pattern, flags); |
| 1581 | const matches = []; |
| 1582 | let match; |
| 1583 | |
| 1584 | if (flags.includes('g')) { |
| 1585 | while ((match = regex.exec(testText)) !== null) { |
| 1586 | matches.push({ |
| 1587 | match: match[0], |
| 1588 | index: match.index, |
| 1589 | groups: match.slice(1) |
| 1590 | }); |
| 1591 | if (match.index === regex.lastIndex) break; |
| 1592 | } |
| 1593 | } else { |
| 1594 | match = regex.exec(testText); |
| 1595 | if (match) { |
| 1596 | matches.push({ |
| 1597 | match: match[0], |
| 1598 | index: match.index, |
| 1599 | groups: match.slice(1) |
| 1600 | }); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | displayVisualResults(matches, testText, regex); |
| 1605 | } catch (e) { |
| 1606 | errorMsg.textContent = '正则表达式语法错误: ' + e.message; |
no test coverage detected