|
935 | 935 | "author": "realvishalrana"
|
936 | 936 | }
|
937 | 937 | ]
|
| 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 | + ] |
938 | 981 | }
|
939 | 982 | ]
|
0 commit comments