react-process-string
The library allows you to process strings with regular expressions in ReactJS.
Installation
Via npm:
npm install react-process-string --save
Syntax
string;
Options should be an array of objects containing regex
and fn
fields.
fn
is a function that takes two arguments: key
, to pass it to a react component and result
— the result of regex executing.
Example usage
const processString = ; Component { let config = regex: /:\/\/\./gim <span key=key> <a target="_blank" href=`://.`>result2result3result4</a>result5 </span> regex: /\./gim <span key=key> <a target="_blank" href=`http://.`>result1result2result3</a>result4 </span> ; let stringWithLinks = "Watch this on youtube.com"; let processed = stringWithLinks; return <div>Hello world! processed</div> ; }
On the user side, processed
will contain clickable links.
Example №2
let users = ourStoreusers;let stringWithUsername = "Hello @efog, how do you feel today?"; let processed = ;
This code allows us to make @usernames
clickable.