|
1 | 1 | <!DOCTYPE HTML> |
2 | 2 | <html> |
3 | | - <head> |
4 | | - <meta http-equiv="content-type" content="text/html; charset=utf-8"> |
5 | | - <meta name="description" content="RegexWorkbench.js is an online open source tool and tester for regular expressions based on javascript. Features are Highlighting, Replace, Ignore Cases, Multiline and a human readable output. [ MIT License ]" /> |
6 | | - <meta name="keywords" content="regex, workbench, regular, expression, regex, regexp, online, tool, tester, editor, javascript" /> |
7 | | - <title>RegexWorkbench.js</title> |
8 | | - <link rel="stylesheet" type="text/css" href="css/main.css" /> |
9 | | - <link rel="stylesheet" type="text/css" href="css/regex-colorizer.css" /> |
10 | | - <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> |
11 | | - <script type="text/javascript" src="js/regex-colorizer.js"></script> |
12 | | - <script type="text/javascript"> |
13 | | - $(function(){ |
14 | | - /// Auto-Run |
15 | | - chooseFunction(); |
| 3 | + <head> |
| 4 | + <meta http-equiv="content-type" content="text/html; charset=utf-8"> |
| 5 | + <meta name="description" content="RegexWorkbench.js is an online open source tool and tester for regular expressions based on javascript. Features are Highlighting, Replace, Ignore Cases, Multiline and a human readable output. [ MIT License ]" /> |
| 6 | + <meta name="keywords" content="regex, workbench, regular, expression, regex, regexp, online, tool, tester, editor, javascript" /> |
| 7 | + <title>RegexWorkbench.js</title> |
| 8 | + <link rel="stylesheet" type="text/css" href="css/main.css" /> |
| 9 | + <link rel="stylesheet" type="text/css" href="css/regex-colorizer.css" /> |
| 10 | + <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> |
| 11 | + <script type="text/javascript" src="js/regex-colorizer.js"></script> |
| 12 | + <script type="text/javascript"> |
| 13 | + $(function(){ |
| 14 | + // Auto-run the regex function on page load |
| 15 | + chooseFunction(); |
16 | 16 |
|
17 | | - /// Events |
18 | | - $("#regexPattern").on('input', chooseFunction); |
19 | | - $("#regexReplace").on('input', chooseFunction); |
20 | | - $("#regexInput").on('input', chooseFunction); |
21 | | - $("#optionMultiline").on("click", chooseFunction); |
22 | | - $("#optionIgnoreCase").on("click", chooseFunction); |
23 | | - $("#forceRefresh").on("click", chooseFunction); |
24 | | - |
| 17 | + // Event listeners for inputs and options |
| 18 | + $("#regexPattern").on('input', chooseFunction); |
| 19 | + $("#regexReplace").on('input', chooseFunction); |
| 20 | + $("#regexInput").on('input', chooseFunction); |
| 21 | + $("#optionMultiline").on("click", chooseFunction); |
| 22 | + $("#optionIgnoreCase").on("click", chooseFunction); |
| 23 | + $("#forceRefresh").on("click", chooseFunction); |
| 24 | + |
| 25 | + // Apply pattern styling when regexPattern loses focus |
| 26 | + $("#regexPattern").focusout(function(){ |
| 27 | + addPaternStyle(); |
| 28 | + if($("#regexPattern").html() == "undefined") { |
| 29 | + $("#regexPattern").html(""); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + // Toggle Replace option |
| 34 | + $("#optionReplace").on("click", function() { |
| 35 | + if(isReplace){ |
| 36 | + isReplace = false; |
| 37 | + } |
| 38 | + else { |
| 39 | + isReplace = true; |
| 40 | + } |
| 41 | + chooseFunction(); |
| 42 | + }); |
| 43 | + |
| 44 | + // New Button: Unique Output - Remove duplicate lines from output |
| 45 | + $("#uniqueOutput").on("click", function(){ |
| 46 | + // Get the content of the output and split it into lines |
| 47 | + var outputText = $("#regexOutput").val(); |
| 48 | + var lines = outputText.split("\n"); |
| 49 | + var uniqueLines = []; |
| 50 | + |
| 51 | + // Add each line only if it is not already in the array |
| 52 | + $.each(lines, function(i, line){ |
| 53 | + if($.inArray(line, uniqueLines) === -1){ |
| 54 | + uniqueLines.push(line); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Join the unique lines and update the output |
| 59 | + $("#regexOutput").val(uniqueLines.join("\n")); |
| 60 | + }); |
25 | 61 |
|
26 | | - $("#regexPattern").focusout(function(){ |
27 | | - addPaternStyle(); |
28 | | - if($("#regexPattern").html() == "undefined") { |
29 | | - $("#regexPattern").html(""); |
30 | | - } |
31 | | - }); |
32 | | - |
33 | | - |
34 | | - $("#optionReplace").on("click", function() { |
35 | | - if(isReplace){ |
36 | | - isReplace = false; |
37 | | - } |
38 | | - else { |
39 | | - isReplace = true; |
40 | | - } |
41 | | - |
42 | | - chooseFunction(); |
43 | | - }); |
44 | | - |
45 | | - |
46 | | - |
| 62 | + // Function to add styling for regex pattern highlighting |
| 63 | + function addPaternStyle() { |
| 64 | + RegexColorizer.addStyleSheet(); |
| 65 | + RegexColorizer.colorizeAll(); |
| 66 | + } |
| 67 | + |
| 68 | + // Choose which function to run based on the selected options |
| 69 | + function chooseFunction() { |
| 70 | + // Set regex modifiers |
| 71 | + var modifiers = "g"; |
| 72 | + if( $('#optionMultiline').prop('checked') ) |
| 73 | + modifiers += "m"; |
| 74 | + if( $('#optionIgnoreCase').prop('checked') ) |
| 75 | + modifiers += "i"; |
47 | 76 |
|
| 77 | + // Run replace or match function based on the Replace option |
| 78 | + if( $("#optionReplace").is(':checked') ) { |
| 79 | + updateReplace(modifiers); |
| 80 | + } |
| 81 | + else { |
| 82 | + updateMatches(modifiers); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Function to handle regex replacement |
| 87 | + function updateReplace(modifiers) { |
| 88 | + var regexPattern = unescape(encodeURIComponent( $("#regexPattern").text() )); |
| 89 | + var regexReplace = unescape(encodeURIComponent( $("#regexReplace").val() )); |
| 90 | + var regexInput = unescape(encodeURIComponent( $("#regexInput").val() )); |
| 91 | + |
| 92 | + var replaceOutput = RWreplace(regexPattern, regexReplace, regexInput, modifiers); |
| 93 | + try { |
| 94 | + $('#regexOutput').val( decodeURIComponent(escape( replaceOutput )) ); |
| 95 | + } |
| 96 | + catch (e) { |
| 97 | + // Error handling if replacement fails |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Function to update and display regex matches |
| 102 | + function updateMatches(modifiers) { |
| 103 | + $("#regexOutput").val(""); |
| 104 | + var regexPattern = unescape(encodeURIComponent( $("#regexPattern").text() )); |
| 105 | + var regexInput = unescape(encodeURIComponent( $("#regexInput").val() )); |
| 106 | + |
| 107 | + var matchArray = RWmatch(regexPattern, regexInput, modifiers); |
| 108 | + var regexOutput = ""; |
48 | 109 |
|
49 | | - /// Functions |
50 | | - // regex-colorizer |
51 | | - function addPaternStyle() { |
52 | | - RegexColorizer.addStyleSheet(); |
53 | | - RegexColorizer.colorizeAll(); |
54 | | - } |
55 | | - |
56 | | - function chooseFunction() { |
57 | | - // Modifiers |
58 | | - var modifiers = "g"; |
59 | | - if( $('#optionMultiline').prop('checked') ) |
60 | | - modifiers += "m"; |
| 110 | + try { |
| 111 | + var i = 0; |
| 112 | + while( i < matchArray.length){ |
| 113 | + if(matchArray[i] != undefined) { |
| 114 | + regexOutput = regexOutput + decodeURIComponent(escape( matchArray[i] )) + "\n"; |
| 115 | + } |
| 116 | + i++; |
| 117 | + } |
| 118 | + $('#regexOutput').val(regexOutput); |
| 119 | + |
| 120 | + // Update the displayed number of matches |
| 121 | + if(matchArray[0] == ""){ |
| 122 | + $("#numberOfMatches").html(0); |
| 123 | + } |
| 124 | + else { |
| 125 | + $("#numberOfMatches").html(i); |
| 126 | + } |
| 127 | + |
| 128 | + } catch (e) { |
| 129 | + // Handle error when regexInput is empty or another error occurs |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Function to retrieve regex matches |
| 134 | + function RWmatch(regexPattern, regexInput, modifiers) { |
| 135 | + if(regexPattern != "" && regexInput != ""){ |
| 136 | + try { |
| 137 | + var pattern = RegExp(regexPattern, modifiers); |
| 138 | + } catch (e) { |
| 139 | + // Error: Not a valid regular expression |
| 140 | + } |
| 141 | + var matchArray = new Array(); |
| 142 | + |
| 143 | + match = pattern.exec(regexInput); |
| 144 | + for (var i = 0; match != null; i++) { |
| 145 | + matchArray[i] = match[1]; |
| 146 | + for(var j = 2; j <= match.length-1; j++){ |
| 147 | + matchArray[i] = matchArray[i] + "," + match[j]; |
| 148 | + } |
| 149 | + |
| 150 | + match = pattern.exec(regexInput); |
| 151 | + |
| 152 | + if(i > 1000000) { |
| 153 | + alert("Stopped operation => Over 1 million data sets."); |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return matchArray; |
| 159 | + } |
| 160 | + } |
61 | 161 |
|
62 | | - if( $('#optionIgnoreCase').prop('checked') ) |
63 | | - modifiers += "i"; |
64 | | - |
65 | | - |
66 | | - // Start function |
67 | | - if( $("#optionReplace").is(':checked') ) { |
68 | | - updateReplace(modifiers); |
69 | | - } |
70 | | - else { |
71 | | - updateMatches(modifiers); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - function updateReplace(modifiers) { |
76 | | - var regexPattern = unescape(encodeURIComponent( $("#regexPattern").text() )); |
77 | | - var regexReplace = unescape(encodeURIComponent( $("#regexReplace").val() )); |
78 | | - var regexInput = unescape(encodeURIComponent( $("#regexInput").val() )); |
79 | | - |
80 | | - var replaceOutput = RWreplace(regexPattern, regexReplace, regexInput, modifiers); |
81 | | - try { |
82 | | - $('#regexOutput').val( decodeURIComponent(escape( replaceOutput )) ); |
83 | | - } |
84 | | - catch (e) { |
85 | | - // Error |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - function updateMatches(modifiers) { |
90 | | - $("#regexOutput").val(""); |
91 | | - var regexPattern = unescape(encodeURIComponent( $("#regexPattern").text() )); |
92 | | - var regexInput = unescape(encodeURIComponent( $("#regexInput").val() )); |
93 | | - |
94 | | - var matchArray = RWmatch(regexPattern, regexInput, modifiers); |
95 | | - var regexOutput = ""; |
96 | | - |
97 | | - try { |
98 | | - var i = 0; |
99 | | - while( i < matchArray.length){ |
100 | | - if(matchArray[i] != undefined) { |
101 | | - regexOutput = regexOutput + decodeURIComponent(escape( matchArray[i] )) + "\n"; |
102 | | - } |
103 | | - i++; |
104 | | - } |
105 | | - $('#regexOutput').val(regexOutput); |
106 | | - |
107 | | - |
108 | | - // Number of matches |
109 | | - if(matchArray[0] == ""){ |
110 | | - $("#numberOfMatches").html(0); |
111 | | - } |
112 | | - else { |
113 | | - $("#numberOfMatches").html(i); |
114 | | - } |
115 | | - |
116 | | - } catch (e) { |
117 | | - // regexInput is empty |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - |
122 | | - function RWmatch(regexPattern, regexInput, modifiers) { |
123 | | - if(regexPattern != "" && regexInput != ""){ |
124 | | - try { |
125 | | - var pattern = RegExp(regexPattern, modifiers); |
126 | | - } catch (e) { |
127 | | - // Error: Not a regular expression |
128 | | - } |
129 | | - var matchArray = new Array(); |
130 | | - |
131 | | - |
132 | | - match = pattern.exec(regexInput); |
133 | | - for (var i = 0; match != null; i++) { |
134 | | - matchArray[i] = match[1]; |
135 | | - for(var j = 2; j <= match.length-1; j++){ |
136 | | - matchArray[i] = matchArray[i] + "," + match[j]; |
137 | | - } |
138 | | - |
139 | | - match = pattern.exec(regexInput); |
140 | | - |
141 | | - |
142 | | - if(i > 1000000) { |
143 | | - alert("Stopped operation => Over 1 million data sets."); |
144 | | - break; |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - |
149 | | - return matchArray; |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - function RWreplace(regexPattern, regexReplace, regexInput, modifiers) { |
154 | | - try { |
155 | | - var pattern = RegExp(regexPattern, modifiers); |
156 | | - } catch (e) { |
157 | | - // Error: Not a regular expression |
158 | | - } |
159 | | - |
160 | | - return regexInput.replace(pattern, regexReplace); |
161 | | - } |
162 | | - |
163 | | - |
164 | | - }); |
165 | | - </script> |
166 | | - </head> |
167 | | - <body> |
168 | | - Regular Expression Pattern:<br /> |
169 | | - <div id="regexPattern" class="regex" contenteditable=""></div> |
170 | | - <button id="forceRefresh">Force refresh</button> |
171 | | - <br /> |
172 | | - Options:<br /> |
173 | | - <input type="checkbox" id="optionReplace">Replace <input type="checkbox" id="optionMultiline">Multiline <input type="checkbox" id="optionIgnoreCase">Ignore Case |
174 | | - <br /> |
175 | | - <br /> |
176 | | - Replace String: |
177 | | - <input type="text" id="regexReplace" /> |
178 | | - <br /> |
179 | | - <br /> |
180 | | - Search String:<br /> |
181 | | - <textarea id="regexInput"></textarea> |
182 | | - <br /> |
183 | | - <br /> |
184 | | - Output String:<br /> |
185 | | - <textarea id="regexOutput" readonly="readonly"></textarea> |
186 | | - <br /> |
187 | | - <br /> |
188 | | - Matches: <span id="numberOfMatches">0</span> |
189 | | - <br /> |
190 | | - <br /> |
191 | | - <center> |
192 | | - RegexWorkbench.js is a editor for regular expressions based on javascript. Features are Highlighting, Replace, Ignore Cases, Multiline and a human readable output. [ MIT License ]<br /> |
193 | | - <a href="https://github.com/Staubteufel/RegexWorkbench.js">Fork me on Github</a> |
194 | | - </center> |
195 | | - </body> |
| 162 | + // Function to perform regex replacement |
| 163 | + function RWreplace(regexPattern, regexReplace, regexInput, modifiers) { |
| 164 | + try { |
| 165 | + var pattern = RegExp(regexPattern, modifiers); |
| 166 | + } catch (e) { |
| 167 | + // Error: Not a valid regular expression |
| 168 | + } |
| 169 | + |
| 170 | + return regexInput.replace(pattern, regexReplace); |
| 171 | + } |
| 172 | + }); |
| 173 | + </script> |
| 174 | + </head> |
| 175 | + <body> |
| 176 | + Regular Expression Pattern:<br /> |
| 177 | + <div id="regexPattern" class="regex" contenteditable=""></div> |
| 178 | + <button id="forceRefresh">Force refresh</button> |
| 179 | + <br /> |
| 180 | + Options:<br /> |
| 181 | + <input type="checkbox" id="optionReplace">Replace |
| 182 | + <input type="checkbox" id="optionMultiline">Multiline |
| 183 | + <input type="checkbox" id="optionIgnoreCase">Ignore Case |
| 184 | + <br /><br /> |
| 185 | + Replace String: |
| 186 | + <input type="text" id="regexReplace" /> |
| 187 | + <br /><br /> |
| 188 | + Search String:<br /> |
| 189 | + <textarea id="regexInput"></textarea> |
| 190 | + <br /><br /> |
| 191 | + Output String:<br /> |
| 192 | + <textarea id="regexOutput" readonly="readonly"></textarea> |
| 193 | + <br /> |
| 194 | + <!-- Button to remove duplicate lines in output --> |
| 195 | + <button id="uniqueOutput">Make Unique Output</button> |
| 196 | + <br /><br /> |
| 197 | + Matches: <span id="numberOfMatches">0</span> |
| 198 | + <br /><br /> |
| 199 | + <center> |
| 200 | + RegexWorkbench.js is an editor for regular expressions based on javascript. Features are Highlighting, Replace, Ignore Cases, Multiline and a human readable output. [ MIT License ]<br /> |
| 201 | + <a href="https://github.com/Staubteufel/RegexWorkbench.js">Fork me on Github</a> |
| 202 | + </center> |
| 203 | + </body> |
196 | 204 | </html> |
0 commit comments