Last active
January 12, 2024 15:40
-
-
Save westc/f5a93d741bddc38299f1da8cf806fd22 to your computer and use it in GitHub Desktop.
Extracts substrings from the given `string` by using `regexp`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extracts substrings from the given `string` by using `regexp`. | |
* @type {{ | |
* (regexp: RegExp, string: string) => RegExpExecArray; | |
* <T>(regexp: RegExp, string: string, handler: (matches: RegExpExecArray) => T) => T; | |
* }} | |
*/ | |
const extract = (regexp, string, handler) => { | |
const matches = regexp.exec(string) ?? []; | |
return handler ? handler(matches) : matches; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extracts substrings from the given `string` by using `regexp`. | |
* @type {{ | |
* (regexp: RegExp, string: string) => Record<string,string>; | |
* <T>(regexp: RegExp, string: string, handler: (matches: RegExpExecArray) => T) => T; | |
* }} | |
*/ | |
const extractGroups = (regexp, string, handler) => { | |
const matches = regexp.exec(string) ?? []; | |
return !handler ? matches.groups ?? {} : handler(matches); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sentences = [ | |
'Hi!', | |
'Hello world!', | |
'Hello crazy world.' | |
]; | |
for (const sentence of sentences) { | |
// Extracts the first and second word from the given string. | |
const [first, second] = extract( | |
/(\S+)\s+(\S+)/, | |
sentence, | |
([match, first, second]) => [first, second] | |
); | |
console.log({first, second}); | |
} | |
// Prints {first: undefined, second: undefined} | |
// Prints {first: "Hello", second: "world!"} | |
// Prints {first: "Hello", second: "crazy"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sentences = [ | |
'Hi!', | |
'Hello world!', | |
'Hello crazy world.' | |
]; | |
for (const sentence of sentences) { | |
// Extracts the first and second word from the given string. | |
const {first, second} = extractGroups( | |
/(?<first>\S+)\s+(?<second>\S+)/, | |
sentence | |
); | |
console.log({first, second}); | |
} | |
// Prints {first: undefined, second: undefined} | |
// Prints {first: "Hello", second: "world!"} | |
// Prints {first: "Hello", second: "crazy"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment