{"id":16853811,"url":"https://github.com/faisalman/re-parse-js","last_synced_at":"2025-03-18T10:25:26.609Z","repository":{"id":193343221,"uuid":"688627179","full_name":"faisalman/re-parse-js","owner":"faisalman","description":"Compose a structured data from unstructured text using regex-based pattern matching","archived":false,"fork":false,"pushed_at":"2023-09-08T06:35:29.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-02T05:31:29.563Z","etag":null,"topics":["parsing-text","pattern-matching","unstructured-data"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/faisalman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"faisalman","patreon":null,"open_collective":"ua-parser-js","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://www.paypal.me/faisalman/"]}},"created_at":"2023-09-07T18:46:32.000Z","updated_at":"2023-09-08T14:25:21.000Z","dependencies_parsed_at":"2023-09-07T20:46:12.102Z","dependency_job_id":"9f0bb824-f00a-4a17-bd6b-fc579c70008d","html_url":"https://github.com/faisalman/re-parse-js","commit_stats":null,"previous_names":["faisalman/re-parse-js"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faisalman%2Fre-parse-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faisalman%2Fre-parse-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faisalman%2Fre-parse-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faisalman%2Fre-parse-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faisalman","download_url":"https://codeload.github.com/faisalman/re-parse-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244200081,"owners_count":20414779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parsing-text","pattern-matching","unstructured-data"],"created_at":"2024-10-13T13:53:19.224Z","updated_at":"2025-03-18T10:25:26.588Z","avatar_url":"https://github.com/faisalman.png","language":"TypeScript","readme":"# REParse.js\nCompose a structured data from unstructured text using regex-based pattern matching. This was originally used in [UAParser.js](https://github.com/faisalman/ua-parser-js/commit/68d124c59c8fd549412ef6df8934eb4cb11f3c07), it basically just loops through a list of regexes, if a match was found then the matched data will be assigned to the result.\n\n```sh\nnpm i re-parse-js\n```\n\n## Methods\n\n`use(re: [RegExp[], REMapper[]][]): REParse`\n\n`parse(str: string): REsult`\n\n## Code Examples\n\n### General Schema\n\n```sh\n[\n    [\n        RegExp[], \n        REMapper[]\n    ],\n    [\n        RegExp[], \n        REMapper[]\n    ],\n    ...\n]\n\nwhere\n\nRegExp[] =\u003e [ a list of RegExp instances that capture the values ]\nREMapper[] =\u003e [ a list of rules on how to map the captured values into result properties ]\n    - string                                # prop = $match \n    - [string, string]                      # prop = string\n    - [string, Function]                    # prop = func($match)\n    - [string, Function, any]               # prop = func($match, arg)\n    - [string, RegExp, string]              # prop = $match.replace(regex, string)\n    - [string, RegExp, string, Function]    # prop = func($match.replace(regex, string))\n```\n\n### 1. Directly assign the captured match into result properties\n\n* Example 1.1: parsing user-agent\n\n```js\nconst remap = [\n    [\n        [\n            /(mozilla)\\/([\\d\\.]+)/i, \n            /(msie)\\/([\\d\\.]+)/i\n        ],\n        ['browser', 'version']\n    ],\n    [\n        [\n            /(opera)\\/((\\d)\\.[\\d\\.]+)/i\n        ], \n        ['browser', 'version', 'major']\n    ]\n];\nconst str1 = 'Mozilla/5.0';\nconst str2 = 'Opera/1.2';\n\nconst re = new REParse();\nre.use(remap);\nre.parse(str1);\n// { browser: 'Mozilla', version: '5.0' }\nre.parse(str2);\n// { browser: 'Opera', version: '1.2', major: '1' }\n```\n\n* Example 1.2: parsing URL\n\n```js\nconst remap = [\n    [\n        [\n            /(https?):\\/\\/(\\w+\\.\\w+)\\/(.*)\\?(.+)/\n        ],\n        ['protocol', 'host', 'path', 'query']\n    ]\n];\nconst urlString = 'https://faisalman.com/?ref=github';\n\nconst re = new REParse(remap);\nre.parse(urlString);\n// { protocol: 'https', host: 'faisalman.com', path: '', query: 'ref=github' }\n```\n\n### 2. Post-process the captured match before assigning to result\n\n#### A. Direct value replacement\n\n```js\nconst remap = [\n    [\n        [\n            /(facebook)\\/(\\d+)/, \n            /(whatsapp)\\/(\\d+)/, \n            /(instagram)\\/(\\d+)/\n        ],\n        [['browser', 'Meta'], 'version'] // Always assign 'Meta' regardless matched value\n    ]\n];\nconst str = 'facebook/100';\n\nnew REParse().use(remap).parse(str);\n// { browser: 'Meta', version: '100' }\n```\n\n#### B. Replace-based value replacement\n\n```js\nconst remap = [\n    [\n        [\n            /(comodo_dragon)\\/(\\d+)/i\n        ],\n        [['browser', /(\\w+)_(\\w{3})\\w+/ig, '$1 $2cula'], 'version'] // Replace captured data, see string.replace\n    ]\n];\nconst str = 'Comodo_Dragon/99';\n\nnew REParse().use(remap).parse(str);\n// { browser: 'Comodo Dracula', version: '99' }\n```\n\n#### C. Function-based value replacement\n\n```js\nconst lowerize = str =\u003e str.toLowerCase();\nconst remap = [\n    [\n        [\n            /(ARM)(64)/\n        ],\n        [['arch', lowerize], 'bitness'] // Pass the captured match into function()\n    ],\n    [\n        [\n            /(x86)-(64)/\n        ],\n        ['arch', 'bitness'] // Direct assignment\n    ]\n];\nconst str1 = 'ARM';\nconst str2 = 'x86-64';\n\nconst re = new REParse()\nre.use(remap)\nre.parse(str1); // { arch: 'arm', bitness: '64' }\nre.parse(str2); // { arch: 'x86', bitness: '64' }\n```\n\n# License\n\nMIT License\n\nCopyright (c) 2023-2025 Faisal Salman \u003c\u003cf@faisalman.com\u003e\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","funding_links":["https://github.com/sponsors/faisalman","https://opencollective.com/ua-parser-js","https://www.paypal.me/faisalman/"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaisalman%2Fre-parse-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaisalman%2Fre-parse-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaisalman%2Fre-parse-js/lists"}