Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/tools/regex-tester/regex-tester.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,79 @@ const regexesData = [
},
],
},
{
regex: '(a)|(b)',
text: 'ab',
flags: 'g',
result: [
{
captures: [
{
name: '1',
value: 'a',
start: 0,
end: 1,
},
],
groups: [],
index: 0,
value: 'a',
},
{
captures: [
{
name: '2',
value: 'b',
start: 1,
end: 2,
},
],
groups: [],
index: 1,
value: 'b',
},
],
},
{
regex: '(a)?b',
text: 'b',
flags: '',
result: [
{
captures: [],
groups: [],
index: 0,
value: 'b',
},
],
},
{
regex: '(?<a>foo)|(?<b>bar)',
text: 'bar',
flags: '',
result: [
{
captures: [
{
name: '2',
value: 'bar',
start: 0,
end: 3,
},
],
groups: [
{
name: 'b',
value: 'bar',
start: 0,
end: 3,
},
],
index: 0,
value: 'bar',
},
],
},
];

describe('regex-tester', () => {
Expand Down
34 changes: 19 additions & 15 deletions src/tools/regex-tester/regex-tester.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
interface RegExpGroupIndices {
[name: string]: [number, number]
[name: string]: [number, number] | undefined
}
interface RegExpIndices extends Array<[number, number]> {
groups: RegExpGroupIndices
interface RegExpIndices extends Array<[number, number] | undefined> {
groups?: RegExpGroupIndices
}
interface RegExpExecArrayWithIndices extends RegExpExecArray {
indices: RegExpIndices
indices?: RegExpIndices
}
interface GroupCapture {
name: string
Expand All @@ -22,31 +22,35 @@ export function matchRegex(regex: string, text: string, flags: string) {
let lastIndex = -1;
const re = new RegExp(regex, flags);
const results = [];
let match = re.exec(text) as RegExpExecArrayWithIndices;
let match = re.exec(text) as RegExpExecArrayWithIndices | null;
while (match !== null) {
if (re.lastIndex === lastIndex || match[0] === '') {
break;
}
const indices = match.indices;
const captures: Array<GroupCapture> = [];
Object.entries(match).forEach(([captureName, captureValue]) => {
if (captureName !== '0' && captureName.match(/\d+/)) {
if (captureName !== '0' && /^\d+$/.test(captureName) && captureValue !== undefined) {
const span = indices?.[Number(captureName)];
captures.push({
name: captureName,
value: captureValue,
start: indices[Number(captureName)][0],
end: indices[Number(captureName)][1],
start: span?.[0] ?? -1,
end: span?.[1] ?? -1,
});
}
});
const groups: Array<GroupCapture> = [];
Object.entries(match.groups || {}).forEach(([groupName, groupValue]) => {
groups.push({
name: groupName,
value: groupValue,
start: indices.groups[groupName][0],
end: indices.groups[groupName][1],
});
if (groupValue !== undefined) {
const span = indices?.groups?.[groupName];
groups.push({
name: groupName,
value: groupValue,
start: span?.[0] ?? -1,
end: span?.[1] ?? -1,
});
}
});
results.push({
index: match.index,
Expand All @@ -55,7 +59,7 @@ export function matchRegex(regex: string, text: string, flags: string) {
groups,
});
lastIndex = re.lastIndex;
match = re.exec(text) as RegExpExecArrayWithIndices;
match = re.exec(text) as RegExpExecArrayWithIndices | null;
}
return results;
}
44 changes: 30 additions & 14 deletions src/tools/regex-tester/regex-tester.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ const unicode = ref(true);
const unicodeSets = ref(false);
const visualizerSVG = ref<ShadowRootExpose>();

const regexValidation = useValidation({
source: regex,
rules: [
{
message: 'Invalid regex: {0}',
validator: value => new RegExp(value),
getErrorMessage: (value) => {
const _ = new RegExp(value);
return '';
},
},
],
watch(unicodeSets, (enabled) => {
if (enabled) {
unicode.value = false;
}
});
const results = computed(() => {

watch(unicode, (enabled) => {
if (enabled) {
unicodeSets.value = false;
}
});

const regexFlags = computed(() => {
let flags = 'd';
if (global.value) {
flags += 'g';
Expand All @@ -49,9 +48,26 @@ const results = computed(() => {
else if (unicodeSets.value) {
flags += 'v';
}
return flags;
});

const regexValidation = useValidation({
source: regex,
watch: [regexFlags],
rules: [
{
message: 'Invalid regex: {0}',
validator: value => new RegExp(value, regexFlags.value),
getErrorMessage: (value) => {
const _ = new RegExp(value, regexFlags.value);
return '';
},
},
],
});
const results = computed(() => {
try {
return matchRegex(regex.value, text.value, flags);
return matchRegex(regex.value, text.value, regexFlags.value);
}
catch (_) {
return [];
Expand Down