Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Strings decorator with: React components, HTML tags etc.

NotificationsYou must be signed in to change notification settings

kas-elvirov/regexify-string

Repository files navigation

npm bundle sizeGitHub release (latest by date)npm-donwloads-per-year


regexify-string

Strings decorator (by regex) with: React components, HTML tags etc.

perfectly works with: strings, html tags, react, react-native

Install

$ npm install --save regexify-string

or

$ yarn add regexify-string

API

regexifyString({ pattern, decorator, input })

pattern

Type:RegExp

decorator

Type:(match: string, index: number, result?: RegExpExecArray) => string | JSX.Element

  • match string you would like to replace/decorate with something
  • index index number of the current match
  • result? RegExpExecArray

NOTE: Try do not forget to use keys for React collections if needed

input

Type:string

Usage

with strings

constresult=regexifyString({pattern:/\[.*?\]/gim,decorator:(match,index)=>{return`<${match}>`;},input:'some [text] with simple example',});console.log(result);// ['some ', '<[text]>', ' with simple example']

with index keys

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]>',        ]    */

with html

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']

with React / React Native components

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',});

with groups

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' />]

License

MIT ©Kas Elvirov

About

Strings decorator with: React components, HTML tags etc.

Topics

Resources

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2026 Movatter.jp