Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

308
Views
How to concat string and add html tag to it?

I have string something like:

Lorem ipsum dolor sit amet, #consectetur and #adipiscing 

I want to add html tag for hashtags which are present in the string. Expected Output is:

Lorem ipsum dolor sit amet, <a href="/users/tag/consectetur">#consectetur</a> and <a href="/users/tag/adipiscing">#adipiscing</a>

I am using javascript string operator split() and join(), but I am not getting the expected output.

    const string =  "Lorem ipsum dolor sit amet, #consectetur and #adipiscing" 
    
    function addHtmlTag(string){
       string = string.split("#").join('<a href="#/users/tag/">#</a>');
    return string
    }
console.log(addHtmlTag(string))

What will be the changes for adding html tag?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use something like this:

const str = "Lorem ipsum dolor sit amet, #consectetur and #adipiscing"

const replacedStr = str.split(" ").map(word => (
  word[0] === "#" ? `<a href="/users/tag/${word.split("#")[1]}">${word}</a>` : word
)).join(" ")

console.log(replacedStr)

This searches for words that starts with "#" and append the a tag based on the word.

about 4 years ago · Juan Pablo Isaza Report

0

It can replace with regexp

    const str = 'Lorem ipsum dolor sit amet, #consectetur and #adipiscing'
    const result = str.replace(/#(\w+)\b/g, '<a href="/users/tag/$1">#$1</a>')
    
    console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

You missed Double qutes ("")

const string =  "Lorem ipsum dolor sit amet, #consectetur and #adipiscing"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!