Skip to content

Commit a7ee81a

Browse files
committed
- update test text in regex tester with more diverse pattern examples
- replace string concatenation with character-by-character marking system for highlighting - fix potential issues with overlapping matches in highlighting - add proper tracking of match start/end positions - ensure HTML escaping is applied correctly - refactor match display code for better readability
1 parent ff9b506 commit a7ee81a

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

tools/regex_tester.html

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,27 @@ <h2 class="card-title">Regular Expression</h2>
605605

606606
<div class="form-group">
607607
<label class="form-label" for="test-text">Test Text</label>
608-
<textarea id="test-text" class="form-input" placeholder="Enter text to test against the regular expression..." aria-label="Test text">As a resident of the UK, I often find myself perusing the internet for information on different countries. Whether it's the US with its vibrant cities, Australia with its stunning beaches, or other countries around the world, I'm always looking to learn more. One thing I've noticed is that when searching for information, I encounter a lot of different formats, from email addresses and phone numbers to postal codes and credit card numbers.
608+
<textarea id="test-text" class="form-input" placeholder="Enter text to test against the regular expression..." aria-label="Test text">RegEx Lab User Guide - April 13, 2025
609609

610-
For example, let's say you want to reach out to someone via email. Using a regular expression, you can match any email address, including extended formats like [email protected]. Similarly, if you're looking for a specific vehicle registered in the UK, you can use a regular expression to match the format of the UK vehicle registration numbers. You can even match phone numbers, whether they're from the US, Australia, or the UK.
610+
Contact Information:
611+
John Smith (Customer ID: ABC123) from Sydney, Australia can be reached at [email protected] or via his alternate email address [email protected]. His US office phone is 555-123-4567, while his direct line is 5551234567. UK colleagues can reach him at 07700900123.
611612

612-
But regular expressions aren't just limited to matching contact information. You can use them to match a wide range of patterns, like IP addresses, URLs, and even dates in the format YYYY-MM-DD. You can even match times in the format HH:MM, and prices with a decimal point (e.g. 10.99).
613+
Technical Details:
614+
The company website is https://www.regex-lab.com with a backup server at http://backup.regex-lab.com/admin. The main server has IP address 192.168.1.1, with additional servers at 10.0.0.1 and 172.16.0.100.
613615

614-
So whether you're trying to match credit card numbers, social security numbers, or even full names, using regular expressions can help you do it quickly and efficiently.</textarea>
616+
Shipping Information:
617+
US customers: Please allow 3-5 business days for delivery to zip codes like 90210 or extended codes such as 90210-1234.
618+
Australian customers: Items shipped to postcodes 2000 (Sydney) or 3000 (Melbourne) arrive within 2 business days.
619+
UK customers: For vehicle deliveries, please provide your registration number (format like AB1234CD) for verification.
620+
621+
Important Dates and Times:
622+
The company was established on 2015-01-15. Our office hours are 09:00 to 17:30 Monday-Friday.
623+
Our quarterly planning meetings happen at 14:00 on 2025-05-20, 2025-08-15, and 2025-11-10.
624+
Prices range from $10.99 to $2,499.99 depending on configuration.
625+
626+
Security Notice:
627+
Never share your Social Security Number (e.g., 123-45-6789) or password (like Password123) with anyone.
628+
Customer support will never ask for your full credit card number (e.g., 4111111111111111).</textarea>
615629
</div>
616630

617631
<div class="form-group">
@@ -963,56 +977,82 @@ <h3 class="mb-2">Pattern Explanation</h3>
963977
// Update stats
964978
resultStats.textContent = `Found ${matches.length} match${matches.length !== 1 ? 'es' : ''} in ${executionTime}ms`;
965979

966-
// Highlight matches in text
967-
let highlightedText = escapeHTML(text);
968-
let lastIndex = 0;
969-
let result = '';
980+
// Create a "marked" array to track which characters are part of matches
981+
// Each position will contain null or an object with match info
982+
const marked = new Array(text.length).fill(null);
983+
984+
// Mark all matches in our tracking array
985+
matches.forEach((match, matchIndex) => {
986+
for (let i = 0; i < match.value.length; i++) {
987+
const pos = match.index + i;
988+
if (pos < text.length) {
989+
marked[pos] = {
990+
matchIndex: matchIndex,
991+
isStart: i === 0,
992+
isEnd: i === match.value.length - 1
993+
};
994+
}
995+
}
996+
});
970997

971-
// Sort matches by index to ensure correct order
972-
matches.sort((a, b) => a.index - b.index);
998+
// Build the HTML by going through the text character by character
999+
let html = '';
1000+
let currentlyInMatch = false;
9731001

974-
for (const match of matches) {
975-
// Add text before the match
976-
result += highlightedText.substring(lastIndex, match.index);
1002+
for (let i = 0; i < text.length; i++) {
1003+
const char = text[i];
1004+
const mark = marked[i];
9771005

978-
// Add the highlighted match
979-
const escapedMatch = escapeHTML(match.value);
980-
if (match.groups.length > 0) {
981-
// If we have capture groups, highlight them differently
982-
result += `<span class="highlight-match">${escapedMatch}</span>`;
983-
} else {
984-
result += `<span class="highlight-match">${escapedMatch}</span>`;
1006+
// Handle opening a new match span
1007+
if (mark && mark.isStart) {
1008+
html += '<span class="highlight-match">';
1009+
currentlyInMatch = true;
9851010
}
9861011

987-
lastIndex = match.index + match.value.length;
1012+
// Add the character (with HTML escaping)
1013+
html += escapeHTML(char);
1014+
1015+
// Handle closing a match span
1016+
if (mark && mark.isEnd && currentlyInMatch) {
1017+
html += '</span>';
1018+
currentlyInMatch = false;
1019+
}
1020+
}
1021+
1022+
// Ensure we close any open spans (safeguard)
1023+
if (currentlyInMatch) {
1024+
html += '</span>';
9881025
}
9891026

990-
// Add remaining text
991-
result += highlightedText.substring(lastIndex);
992-
resultContent.innerHTML = result;
1027+
// Set the result
1028+
resultContent.innerHTML = html;
9931029

9941030
// Display matches detail
9951031
if (matches.length > 0) {
9961032
matchesList.innerHTML = '';
1033+
1034+
// Sort matches by index for display
1035+
matches.sort((a, b) => a.index - b.index);
1036+
9971037
matches.forEach((match, index) => {
9981038
const matchItem = document.createElement('div');
9991039
matchItem.className = 'match-item';
10001040

10011041
let matchHTML = `
1002-
<div class="match-index">Match ${index + 1} (index: ${match.index})</div>
1003-
<div class="match-value">${escapeHTML(match.value)}</div>
1004-
`;
1042+
<div class="match-index">Match ${index + 1} (index: ${match.index})</div>
1043+
<div class="match-value">${escapeHTML(match.value)}</div>
1044+
`;
10051045

10061046
if (match.groups.length > 0) {
10071047
matchHTML += '<div class="match-groups">';
10081048
match.groups.forEach((group, groupIndex) => {
10091049
if (group !== undefined) {
10101050
matchHTML += `
1011-
<div class="group-item">
1012-
<div class="group-number">${groupIndex + 1}</div>
1013-
<div class="group-value">${escapeHTML(group)}</div>
1014-
</div>
1015-
`;
1051+
<div class="group-item">
1052+
<div class="group-number">${groupIndex + 1}</div>
1053+
<div class="group-value">${escapeHTML(group)}</div>
1054+
</div>
1055+
`;
10161056
}
10171057
});
10181058
matchHTML += '</div>';

0 commit comments

Comments
 (0)