Skip to content

Commit ef43cbd

Browse files
committed
Feat(data): Add JS regex match utility function to snippets
1 parent e7a1093 commit ef43cbd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

public/data/javascript.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,5 +935,48 @@
935935
"author": "realvishalrana"
936936
}
937937
]
938+
},
939+
{
940+
"categoryName": "Regular expression",
941+
"snippets": [
942+
{
943+
"title": "Regex Match Utility Function",
944+
"description": "Enhanced regular expression matching utility.",
945+
"code": [
946+
"/**",
947+
"* @param {string | number} input",
948+
"* The input string to match",
949+
"* @param {regex | string} expression",
950+
"* Regular expression",
951+
"* @param {string} flags",
952+
"* Optional Flags",
953+
"*",
954+
"* @returns {array}",
955+
"* [{",
956+
"* match: '...',",
957+
"* matchAtIndex: 0,",
958+
"* capturedGroups: [ '...', '...' ]",
959+
"* }]",
960+
"*/",
961+
"function regexMatch(input, expression, flags = 'g') {",
962+
" let regex =",
963+
" expression instanceof RegExp",
964+
" ? expression",
965+
" : new RegExp(expression, flags);",
966+
" let matches = input.matchAll(regex);",
967+
" matches = [...matches];",
968+
" return matches.map((item) => {",
969+
" return {",
970+
" match: item[0],",
971+
" matchAtIndex: item.index,",
972+
" capturedGroups: item.length > 1 ? item.slice(1) : undefined,",
973+
" };",
974+
" });",
975+
"}"
976+
],
977+
"tags": ["javascript","regex"],
978+
"author": "aumirza"
979+
}
980+
]
938981
}
939982
]

0 commit comments

Comments
 (0)