{"id":19838143,"url":"https://github.com/textlint/regexp-string-matcher","last_synced_at":"2025-05-01T18:31:05.243Z","repository":{"id":32461227,"uuid":"134276195","full_name":"textlint/regexp-string-matcher","owner":"textlint","description":"Regexp-like string(\"/github/i\") matcher library.","archived":false,"fork":false,"pushed_at":"2022-06-28T13:05:46.000Z","size":187,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-12T21:54:05.989Z","etag":null,"topics":["javascript","matcher","regexp","string","util"],"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/textlint.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-05-21T13:54:10.000Z","updated_at":"2022-06-28T13:22:57.000Z","dependencies_parsed_at":"2022-08-30T15:21:18.901Z","dependency_job_id":null,"html_url":"https://github.com/textlint/regexp-string-matcher","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/textlint%2Fregexp-string-matcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textlint%2Fregexp-string-matcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textlint%2Fregexp-string-matcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textlint%2Fregexp-string-matcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/textlint","download_url":"https://codeload.github.com/textlint/regexp-string-matcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224269997,"owners_count":17283649,"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":["javascript","matcher","regexp","string","util"],"created_at":"2024-11-12T12:16:53.284Z","updated_at":"2024-11-12T12:16:53.937Z","avatar_url":"https://github.com/textlint.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @textlint/regexp-string-matcher [![Actions Status: test](https://github.com/textlint/regexp-string-matcher/workflows/test/badge.svg)](https://github.com/textlint/regexp-string-matcher/actions?query=workflow%3A\"test\")\n\nRegexp-like string matcher library.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n    npm install @textlint/regexp-string-matcher\n\n## Usage\n\nInterface:\n\n```ts\nexport interface matchPatternResult {\n    match: string;\n    startIndex: number;\n    endIndex: number;\n}\n/**\n * Match regExpLikeStrings and return matchPatternResults\n * @param text target text\n * @param regExpLikeStrings an array of pattern string\n */\nexport declare const matchPatterns: (text: string, regExpLikeStrings: string[]) =\u003e matchPatternResult[];\n```\n\nExample:\n\n```js\nimport { matchPatterns } from \"@textlint/regexp-string-matcher\";\nconst inputText = `\nGitHub is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\nGitHub launched in 2018-04-10.`;\n// RegExp like strings\nconst inputPatterns = [\n    \"git\", // =\u003e /git/g\n    \"/github/i\", // =\u003e /github/ig\n    \"/(\\\\d{4})-(\\\\d{2})-(\\\\d{2})/\" // =\u003e /\\d{4}-\\d{2}-\\d{2}/g\n];\n\nconst results = matchPatterns(inputText, inputPatterns);\nassert.deepStrictEqual(results, [\n    { match: \"GitHub\", startIndex: 1, endIndex: 7, captures: [] },\n    { match: \"git\", startIndex: 65, endIndex: 68, captures: [] },\n    { match: \"GitHub\", startIndex: 107, endIndex: 113, captures: [] },\n    { match: \"2018-04-10\", startIndex: 126, endIndex: 136, captures: [\"2018\", \"04\", \"10\"] }\n]);\n```\n\n## RegExp-like String\n\nThis library aim to represent RegExp in JSON and use it for ignoring words.\n`g`(global) flag and `u`(unicode) is added by default.\n\n| Input        | Ouput   | Note                                       |\n|--------------|---------|--------------------------------------------|\n| `\"str\"`      | `/str/gu` | convert string to regexp with global       |\n| `\"/str/\"`    | `/str/gu` |                                            |\n| `\"/str/g\"`   | `/str/gu` | Duplicated `g` is just ignored             |\n| `\"/str/i\"`   | `/str/igu` |                                            |\n| `\"/str/u\"`   | `/str/ug` |                                            |\n| `\"/str/m\"`   | `/str/mgu` |                                            |\n| `\"/str/y\"`   | `/str/ygu` |                                            |\n| ---          | ---     | ---                                        |\n| `\"/\\\\d+/\"`   | `/\\d+/gu` | You should escape meta character like `\\d` |\n| `\"/(\\\\d+)/\"` | `/\\d+/gu` | You can use capture                        |\n\n:warning: You should escape meta character like `\\d` in RegExp-like string.\n\nFor example, If you want to write `\\w`(any word) in RegExp-like string, you should escape `\\w` to `\\\\w`.\n\n- [Regular Expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters)\n\nText:\n\n```\nThis is a pen.\n```\n\nRegExp-like String:\n\n```json\n[\n    \"/a (\\\\w+)/\"\n]\n```\n\nResults:\n\n```\n[ { match: 'a pen', startIndex: 8, endIndex: 13, captures: [\"pen\"] } ]\n```\n\n## Examples\n\n### string\n\n**text:**\n```markdown\nGitHub is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\nGitHub launched in 2018-04-10.\n```\n\n**pattern:**\n\n```json\n[\n  \"GitHub\"\n]\n```\n\n**results:** 2 hits\n```markdown\n**GitHub** is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\n**GitHub** launched in 2018-04-10.\n```\n\n### Ignore Case match\n\n**text:**\n```markdown\nGitHub is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\nGitHub launched in 2018-04-10.\n```\n\n**pattern:**\n\n```json\n[\n  \"/git/i\"\n]\n```\n\n**results:**: 3 hits\n```markdown\n**Git**Hub is a web-based hosting service for version control using **git**.\nIt is mostly used for computer code.\n**Git**Hub launched in 2018-04-10.\n```\n\n### Special character\n\nYou should escape special charactor like `\\d` in RegExp-like string.\n\n**text:**\n```markdown\nGitHub is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\nGitHub launched in 2018-04-10.\n```\n\n**pattern:**\n```json\n[\n  \"/\\\\d{4}-\\\\d{2}-\\\\d{2}/\"\n]\n```\n\n**results:**: 1 hit\n```markdown\nGitHub is a web-based hosting service for version control using git.\nIt is mostly used for computer code.\nGitHub launched in **2018-04-10**.\n```\n\n### Multi-line\n\n\n**text:**\n```markdown\n===START===\n1st inline text.\n===END===\n\n===START===\n2nd inline text.\n===END===\n```\n\n**pattern:**\n```json\n[\n  \"/===START===[\\\\s\\\\S]*?===END===/m\"\n]\n\n```\n\n**results:**: 2 hits\n```markdown\n**===START===\n1st inline text.\n===END===**\n\n**===START===\n2nd inline text.\n===END===**\n```\n\n\nFor more details, see [test/snapshots](./test/snapshots)\n\n### Escape bracket\n\n**text:**\n```markdown\nTODO [Issue #1]: it will be fixed\n```\n\n**patterns:**\n```json\n[\n  \"/TODO \\\\[Issue #\\\\d+\\\\]:/i\"\n]\n```\n\n:memo: You should escape bracket both. `\\\\[` and `\\\\]`,\n\n**results:**\n\n```\n**TODO [Issue #1]:** it will be fixed\n```\n\n\n## Changelog\n\nSee [Releases page](https://github.com/textlint/regexp-string-matcher/releases).\n\n## Running tests\n\nInstall devDependencies and Run `npm test`:\n\n    npm i -d \u0026\u0026 npm test\n\n### How to add snapshot tests?\n\n1. Create new dir to `./snapshots/\u003cname\u003e/`\n2. Add `input.txt` and `input-patterns.json`\n3. Run `npm run test:updateSnapshot`\n4. You should verify the output results manually\n5. Run `npm test` and pass it\n5. Commit it\n\n## Contributing\n\nPull requests and stars are always welcome.\n\nFor bugs and feature requests, [please create an issue](https://github.com/textlint/regexp-string-matcher/issues).\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request :D\n\n## Author\n\n- [github/azu](https://github.com/azu)\n- [twitter/azu_re](https://twitter.com/azu_re)\n\n## License\n\nMIT © azu\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextlint%2Fregexp-string-matcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftextlint%2Fregexp-string-matcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextlint%2Fregexp-string-matcher/lists"}