diff --git a/public/data/javascript.json b/public/data/javascript.json index 9fb3dc05..d3c6ddae 100644 --- a/public/data/javascript.json +++ b/public/data/javascript.json @@ -313,5 +313,45 @@ "author": "dostonnabotov" } ] + }, + { + "categoryName": "Regular expression", + "snippets": [ + { + "title": "Regex Match Utility Function", + "description": "Enhanced regular expression matching utility.", + "code": [ + "/**", + "* @param {string | number} input", + "* The input string to match", + "* @param {regex | string} expression", + "* Regular expression", + "* @param {string} flags", + "* Optional Flags", + "*", + "* @returns {array}", + "* [{", + "match: '...',", + "matchAtIndex: 0,", + "capturedGroups: [ '...', '...' ]", + "}]", + "*/", + "function regexMatch(input, expression, flags = \"g\") {", + "let regex = expression instanceof RegExp ? expression : new RegExp(expression, flags)", + "let matches = input.matchAll(regex)", + "matches = [...matches]", + "return matches.map(item => {", + "return {", + "match: item[0],", + "matchAtIndex: item.index,", + "capturedGroups: item.length > 1 ? item.slice(1) : undefined", + "}", + "})", + "}" + ], + "tags": ["javascript","regex"], + "author": "aumirza" + } + ] } -] +] \ No newline at end of file