Last active
December 23, 2021 17:46
-
-
Save dimon-durak/479cd52c0cb4ae7181906df45d77679d to your computer and use it in GitHub Desktop.
Parse string
This file contains hidden or 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
| let parsingSplitter = '~'; | |
| let strForParsing = 'Sherlock Holmes~221B Baker Street~London~UK'; | |
| let parsingKeys = ['name', 'address', 'city', 'country']; | |
| const parse = (str, keys, splitter) => { | |
| const reStr = keys.reduce((s, k, i, a) => `${s}(?<${k}>.*)${ i + 1 < a.length ? splitter : ''}`,''); | |
| const RE = new RegExp(reStr); | |
| const { groups:parsedData } = RE.exec(str); | |
| return parsedData | |
| }; | |
| parse(strForParsing, parsingKeys, parsingSplitter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment