{"id":19475467,"url":"https://github.com/artdecocode/mismatch","last_synced_at":"2025-02-25T15:47:03.966Z","repository":{"id":57298322,"uuid":"141777719","full_name":"artdecocode/mismatch","owner":"artdecocode","description":"A JavaScript package to return captured groups of a regular expression as objects in an array.","archived":false,"fork":false,"pushed_at":"2019-04-23T15:08:44.000Z","size":57,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-08T13:45:40.389Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/artdecocode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-21T03:20:57.000Z","updated_at":"2021-11-14T00:31:12.000Z","dependencies_parsed_at":"2022-09-06T03:31:13.520Z","dependency_job_id":null,"html_url":"https://github.com/artdecocode/mismatch","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fmismatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fmismatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fmismatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artdecocode%2Fmismatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artdecocode","download_url":"https://codeload.github.com/artdecocode/mismatch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240699223,"owners_count":19843509,"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":[],"created_at":"2024-11-10T19:33:00.621Z","updated_at":"2025-02-25T15:47:03.944Z","avatar_url":"https://github.com/artdecocode.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mismatch\n\n[![npm version](https://badge.fury.io/js/mismatch.svg)](https://npmjs.org/package/mismatch)\n\n`mismatch` is a JavaScript package to return captured groups of a regular expression as objects in an array.\n\n```sh\nyarn add mismatch\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/0.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## Table Of Contents\n\n- [Table Of Contents](#table-of-contents)\n- [API](#api)\n  * [`mismatch(re: RegExp, string: string, keys: string[], addPosition?: boolean)`](#mismatchre-regexpstring-stringkeys-stringaddposition-boolean-void)\n- [Copyright](#copyright)\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/1.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## API\n\nThe package is available by importing its default function:\n\n```js\nimport mismatch from 'mismatch'\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/2.svg?sanitize=true\" width=\"25\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n### `mismatch(`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`re: RegExp,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`string: string,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`keys: string[],`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`addPosition?: boolean,`\u003cbr/\u003e`): void`\n\nThe function will attempt to find all matches for a given regular expression in a string using `.replace()` method, construct an object consisting of captured groups based on supplied keys, and return those objects as an array. It has an advantage over iterating over `while(RegExp.exec(string))` pattern because it does not modify the regular expression's `lastIndex` property.\n\n```js\n/* yarn example/ */\nimport mismatch from 'mismatch'\n\nconst re = /(\\w+)=\"(.+?)\"/g\nconst string = `\n\u003cscript\n  crossorigin=\"anonymous\"\n  src=\"https://static.npmjs.com/commons.js\"\n  integrity=\"sha512-example/rhb92Zdom+ix+AYtqZ7C1DlLKEA==\"\n\u003e\u003c/script\u003e\n`\nconst keys = ['attribute', 'value']\nconst res = mismatch(re, string, keys)\nconsole.log(JSON.stringify(res, null, 2))\n```\n\n```json\n[\n  {\n    \"attribute\": \"crossorigin\",\n    \"value\": \"anonymous\"\n  },\n  {\n    \"attribute\": \"src\",\n    \"value\": \"https://static.npmjs.com/commons.js\"\n  },\n  {\n    \"attribute\": \"integrity\",\n    \"value\": \"sha512-example/rhb92Zdom+ix+AYtqZ7C1DlLKEA==\"\n  }\n]\n```\n\nIf an optional capturing group was not found, its key will not be included as part of the object. Also, if there are more captured groups than keys, they will also not be included.\n\n```js\n/* yarn example/extra.js */\nimport mismatch from 'mismatch'\n\nconst re = /(?: type=\"(.+?)\")?\\s+crossorigin=\"(.+?)\"\\s+src=\"(.+?)\"/g\nconst string = `\n\u003cscript\n  crossorigin=\"anonymous\"\n  src=\"https://static.npmjs.com/commons.js\"\n  integrity=\"sha512-example/rhb92Zdom+ix+AYtqZ7C1DlLKEA==\"\n\u003e\u003c/script\u003e\n`\nconst keys = ['type', 'crossorigin']\nconst res = mismatch(re, string, keys)\nconsole.log(JSON.stringify(res, null, 2))\n```\n```json\n[\n  {\n    \"crossorigin\": \"anonymous\"\n  }\n]\n```\n\nTo additionally add the positions at which the matches was found, the last argument, `addPositions` should be set to _true_.\n\n```js\n/* yarn example/ */\nimport mismatch from 'mismatch'\n\nconst re = /(\\w+)=\"(.+?)\"/g\nconst string = `\n\u003cscript\n  crossorigin=\"anonymous\"\n  src=\"https://static.npmjs.com/commons.js\"\n  integrity=\"sha512-example/rhb92Zdom+ix+AYtqZ7C1DlLKEA==\"\n\u003e\u003c/script\u003e\n`\nconst keys = ['attribute', 'value']\nconst res = mismatch(re, string, keys, true)\nconsole.log(JSON.stringify(res, null, 2))\n```\n```json\n[\n  {\n    \"position\": 11,\n    \"attribute\": \"crossorigin\",\n    \"value\": \"anonymous\"\n  },\n  {\n    \"position\": 37,\n    \"attribute\": \"src\",\n    \"value\": \"https://static.npmjs.com/commons.js\"\n  },\n  {\n    \"position\": 81,\n    \"attribute\": \"integrity\",\n    \"value\": \"sha512-example/rhb92Zdom+ix+AYtqZ7C1DlLKEA==\"\n  }\n]\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/3.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n## Copyright\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003e\n      \u003ca href=\"https://artd.eco\"\u003e\n        \u003cimg src=\"https://raw.githubusercontent.com/wrote/wrote/master/images/artdeco.png\" alt=\"Art Deco\" /\u003e\n      \u003c/a\u003e\n    \u003c/th\u003e\n    \u003cth\u003e© \u003ca href=\"https://artd.eco\"\u003eArt Deco\u003c/a\u003e   2019\u003c/th\u003e\n    \u003cth\u003e\n      \u003ca href=\"https://www.technation.sucks\" title=\"Tech Nation Visa\"\u003e\n        \u003cimg src=\"https://raw.githubusercontent.com/artdecoweb/www.technation.sucks/master/anim.gif\"\n          alt=\"Tech Nation Visa\" /\u003e\n      \u003c/a\u003e\n    \u003c/th\u003e\n    \u003cth\u003e\u003ca href=\"https://www.technation.sucks\"\u003eTech Nation Visa Sucks\u003c/a\u003e\u003c/th\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\u003cimg src=\".documentary/section-breaks/-1.svg?sanitize=true\"\u003e\u003c/a\u003e\u003c/p\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartdecocode%2Fmismatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartdecocode%2Fmismatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartdecocode%2Fmismatch/lists"}