- Notifications
You must be signed in to change notification settings - Fork3
Strings decorator with: React components, HTML tags etc.
NotificationsYou must be signed in to change notification settings
kas-elvirov/regexify-string
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Strings decorator (by regex) with: React components, HTML tags etc.
perfectly works with: strings, html tags, react, react-native
$ npm install --save regexify-stringor
$ yarn add regexify-stringType:RegExp
Type:(match: string, index: number, result?: RegExpExecArray) => string | JSX.Element
matchstring you would like to replace/decorate with somethingindexindex number of the current matchresult?RegExpExecArray
NOTE: Try do not forget to use keys for React collections if needed
Type:string
constresult=regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{return`<${match}>`;},input:'some [text] with simple example',});console.log(result);// ['some ', '<[text]>', ' with simple example']
constresult=regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{switch(index){case0:return`<FIRST${match}>`;case1:return`<SECOND${match}>`;case2:return`<THIRD${match}>`;default:returnmatch;}},input:'Important text with [first link] and [second link] and much more [links]',});console.log(result);/* [ 'Important text with ', '<FIRST [first link]>', ' and ', '<SECOND [second link]>', ' and much more ', '<THIRD [links]>', ] */
constresult=regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{return<span>{match}</span>;},input:'some [text] with simple example',});console.log(result);// ['some ', <span>[text]</span>, ' with simple example']
regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{return(<Linkto={SOME_ROUTE}onClick={onClick}>{match}</Link>);},input:someVariablWithData,});
regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{return<React.Fragment>{match}</React.Fragment>;},input:'some [text] with simple example',});
constresult=regexifyString({pattern:/\[(?<id>.+)\]\{(?<name>.+)\}/g,decorator:(match,index,result)=>{return(<divid={String(result?.[2])}title={result?.[1]}/>);},input:'a[b]{c}',});console.log(result);// ['a', <div id='c' title='b' />]
MIT ©Kas Elvirov
About
Strings decorator with: React components, HTML tags etc.
Topics
Resources
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
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.