Created
November 9, 2021 11:36
-
-
Save viduraperera/57070c193558d83a88754bc244fef3fc to your computer and use it in GitHub Desktop.
full regex code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, {useState} from "react"; | |
| function App(){ | |
| const[data, setData] = useState(null); | |
| const[print, setPrint] = useState(false); | |
| function getData(val){ | |
| if(!val) return; | |
| const URL_REGEX = /(((https?:\/\/)|(www\.))[^\s]+)/g; | |
| let textSubject = val.target.value; | |
| return setData(textSubject.replace(URL_REGEX, (url)=>{ | |
| let hyperLink = url; | |
| if(!hyperLink.match('^https?:\/\/')){ | |
| hyperLink = 'http://' + hyperLink; | |
| } | |
| setPrint(false) | |
| return `<a href="${hyperLink.toLowerCase().lastIndexOf('www.', 0) === 0 ? `//${hyperLink}` : hyperLink}">${hyperLink}</a>` | |
| })); | |
| } | |
| return( | |
| <div> | |
| { | |
| print? | |
| <h1> | |
| <div dangerouslySetInnerHTML={{ __html: data }} /> | |
| </h1> | |
| :null | |
| } | |
| <input type="text" onChange={getData}></input> | |
| <button onClick={()=>setPrint(true)}>Print Data</button> | |
| </div> | |
| ); | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment