Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Regexp-like string("/github/i") matcher library.
License
textlint/regexp-string-matcher
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Regexp-like string matcher library.
Install withnpm:
npm install @textlint/regexp-string-matcher
Interface:
exportinterfacematchPatternResult{match:string;startIndex:number;endIndex:number;}/** * Match regExpLikeStrings and return matchPatternResults *@param text target text *@param regExpLikeStrings an array of pattern string */exportdeclareconstmatchPatterns:(text:string,regExpLikeStrings:string[])=>matchPatternResult[];
Example:
import{matchPatterns}from"@textlint/regexp-string-matcher";constinputText=`GitHub is a web-based hosting service for version control using git.It is mostly used for computer code.GitHub launched in 2018-04-10.`;// RegExp like stringsconstinputPatterns=["git",// => /git/g"/github/i",// => /github/ig"/(\\d{4})-(\\d{2})-(\\d{2})/"// => /\d{4}-\d{2}-\d{2}/g];constresults=matchPatterns(inputText,inputPatterns);assert.deepStrictEqual(results,[{match:"GitHub",startIndex:1,endIndex:7,captures:[]},{match:"git",startIndex:65,endIndex:68,captures:[]},{match:"GitHub",startIndex:107,endIndex:113,captures:[]},{match:"2018-04-10",startIndex:126,endIndex:136,captures:["2018","04","10"]}]);
This library aim to represent RegExp in JSON and use it for ignoring words.g
(global) flag andu
(unicode) is added by default.
Input | Ouput | Note |
---|---|---|
"str" | /str/gu | convert string to regexp with global |
"/str/" | /str/gu | |
"/str/g" | /str/gu | Duplicatedg is just ignored |
"/str/i" | /str/igu | |
"/str/u" | /str/ug | |
"/str/m" | /str/mgu | |
"/str/y" | /str/ygu | |
--- | --- | --- |
"/\\d+/" | /\d+/gu | You should escape meta character like\d |
"/(\\d+)/" | /\d+/gu | You can use capture |
\d
in RegExp-like string.
For example, If you want to write\w
(any word) in RegExp-like string, you should escape\w
to\\w
.
Text:
This is a pen.
RegExp-like String:
["/a (\\w+)/"]
Results:
[ { match: 'a pen', startIndex: 8, endIndex: 13, captures: ["pen"] } ]
text:
GitHub is a web-based hosting service for version control using git.It is mostly used for computer code.GitHub launched in 2018-04-10.
pattern:
["GitHub"]
results: 2 hits
**GitHub** is a web-based hosting service for version control using git.It is mostly used for computer code.**GitHub** launched in 2018-04-10.
text:
GitHub is a web-based hosting service for version control using git.It is mostly used for computer code.GitHub launched in 2018-04-10.
pattern:
["/git/i"]
results:: 3 hits
**Git**Hub is a web-based hosting service for version control using**git**.It is mostly used for computer code.**Git**Hub launched in 2018-04-10.
You should escape special charactor like\d
in RegExp-like string.
text:
GitHub is a web-based hosting service for version control using git.It is mostly used for computer code.GitHub launched in 2018-04-10.
pattern:
["/\\d{4}-\\d{2}-\\d{2}/"]
results:: 1 hit
GitHub is a web-based hosting service for version control using git.It is mostly used for computer code.GitHub launched in**2018-04-10**.
text:
===START===1st inline text.===END======START===2nd inline text.===END===
pattern:
["/===START===[\\s\\S]*?===END===/m"]
results:: 2 hits
**===START===1st inline text.===END===****===START===2nd inline text.===END===**
For more details, seetest/snapshots
text:
TODO[Issue#1]: it will be fixed
patterns:
["/TODO\\[Issue #\\d+\\]:/i"]
📝 You should escape bracket both.\\[
and\\]
,
results:
**TODO [Issue #1]:** it will be fixed
SeeReleases page.
Install devDependencies and Runnpm test
:
npm i -d && npm test
- Create new dir to
./snapshots/<name>/
- Add
input.txt
andinput-patterns.json
- Run
npm run test:updateSnapshot
- You should verify the output results manually
- Run
npm test
and pass it - Commit it
Pull requests and stars are always welcome.
For bugs and feature requests,please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © azu
About
Regexp-like string("/github/i") matcher library.
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.