Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to wrap words with anchor tags in react if string contains certain symbols

Hey guys am working on blog content with #hashtags and @mentions,i want a to check for all the words in a string if this words have @mention or #tags wrap them with anchor tags, so incase i have something like "hello @watchdog are you with @example and #trends #football"

so i want to wrap to have something like @watchdog @exmaple #exmaple

am using react js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export const text = (text) =>{
const [linktext,setLinkText] =  useState('')

useEffect(()=>{
},[])

return (
   <div>{text}</div>
)
}

>Solution :

export const text = (text) =>{
const [linktext,setLinkText] =  useState("hello @watchdog are you with @example and #trends #football")

useEffect(()=>{
},[])

return (
   <div>{linktext.split(" ").map(word => (
         (word.includes("@") || word.includes("#"))
           ?
         <a>{word} </a>
        :
        <p>{word}</p>
    ))}</div>
)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading