Skip to content

Feat(data): add regex match utility function snippet in JS #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 41 additions & 1 deletion public/data/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
]