Skip to content

Commit 4a7484b

Browse files
authored
Feat(data): add regex match utility functtion snippet in js
1 parent 600723c commit 4a7484b

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

public/data/javascript.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,45 @@
313313
"author": "dostonnabotov"
314314
}
315315
]
316+
},
317+
{
318+
"categoryName": "Regular expression",
319+
"snippets": [
320+
{
321+
"title": "Regex Match Utility Function",
322+
"description": "Enhanced regular expression matching utility.",
323+
"code": [
324+
"/**",
325+
"* @param {string | number} input",
326+
"* The input string to match",
327+
"* @param {regex | string} expression",
328+
"* Regular expression",
329+
"* @param {string} flags",
330+
"* Optional Flags",
331+
"*",
332+
"* @returns {array}",
333+
"* [{",
334+
"match: '...',",
335+
"matchAtIndex: 0,",
336+
"capturedGroups: [ '...', '...' ]",
337+
"}]",
338+
"*/",
339+
"function regexMatch(input, expression, flags = \"g\") {",
340+
"let regex = expression instanceof RegExp ? expression : new RegExp(expression, flags)",
341+
"let matches = input.matchAll(regex)",
342+
"matches = [...matches]",
343+
"return matches.map(item => {",
344+
"return {",
345+
"match: item[0],",
346+
"matchAtIndex: item.index,",
347+
"capturedGroups: item.length > 1 ? item.slice(1) : undefined",
348+
"}",
349+
"})",
350+
"}"
351+
],
352+
"tags": ["javascript","regex"],
353+
"author": "aumirza"
354+
}
355+
]
316356
}
317-
]
357+
]

0 commit comments

Comments
 (0)