A better version of regex 101
> doesn't make a group
1 file changed, +14 -7
+14-7src/builder.js
| @@ -3,11 +3,13 @@ const parseBuilder = (str) => { | ||
| 3 | 3 | const ref = {}; |
| 4 | 4 | for (let line of lines) { |
| 5 | 5 | if (!line.trim()) continue; |
| 6 | - const r = /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??):(?<regex>.*)/.exec( | |
| 7 | - line | |
| 8 | - ); | |
| 6 | + const r = | |
| 7 | + /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??)(?<type>[:>])(?<regex>.*)/.exec( | |
| 8 | + line | |
| 9 | + ); | |
| 9 | 10 | if (!r) return ""; |
| 10 | 11 | const lineData = r.groups; |
| 12 | + console.log(lineData); | |
| 11 | 13 | const tabs = lineData.tabs.length; |
| 12 | 14 | if (!ref[tabs]) ref[tabs] = [lineData]; |
| 13 | 15 | else ref[tabs].push(lineData); |
| @@ -29,10 +31,15 @@ const buildToRegex = (build) => { | ||
| 29 | 31 | const buildToRegexRecursion = (build, re) => { |
| 30 | 32 | if (!build?.length) return re; |
| 31 | 33 | const val = build.shift(); |
| 32 | - if (val.name) re += `(?P<${val.name}>${val.regex}`; | |
| 33 | - else re += `(?:${val.regex}`; | |
| 34 | - if (val.children) re += buildToRegexRecursion(val.children, ""); | |
| 35 | - re += `)${val.optional}`; | |
| 34 | + if (val.type === ":") { | |
| 35 | + if (val.name) re += `(?P<${val.name}>${val.regex}`; | |
| 36 | + else re += `(?:${val.regex}`; | |
| 37 | + if (val.children) re += buildToRegexRecursion(val.children, ""); | |
| 38 | + re += `)${val.optional}`; | |
| 39 | + } else if (val.type === ">") { | |
| 40 | + re += val.regex; | |
| 41 | + if (val.children) re += buildToRegexRecursion(val.children, ""); | |
| 42 | + } | |
| 36 | 43 | |
| 37 | 44 | return buildToRegexRecursion(build, re); |
| 38 | 45 | }; |