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

213
Views
Javascript/Jquery: replace string pattern with link

I have a table on a page, where there are text entries, and have one or more "hashtags", which I want to identify via JavaScript/JQuery and replace them with a link.

Currently I am using the following regex selector for hashtags: /#([a-zA-Z0-9]+)/g

I have been experimenting with replaceWith and replace methods of JQuery, but I couldn't achieve the desired result yet.

Example entries on the page:

<td class="entry">Had meeting with client #Project1</td>

This is my desired output (after manipulating via JavaScript/JQuery):

`<td class="entry">Had meeting with client <a href="search.php?`query=#Project1">#Project1</a></td>

Does someone help out with a function/for this? As my code is not close to working I am I choosing not to post the code..

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

0

You can do something like this using JavaScript:

const entries = document.querySelectorAll(".entry");

entries.forEach((entry) => {
  entry.innerText.match(/#([a-zA-Z0-9]+)/g).forEach((hash) => {
    entry.innerHTML = entry.innerHTML.replace(
      hash,
      `<a href="search.php?query=${hash}">${hash}</a>`
    );
  });
});
<p class="entry">Had meeting with client #Project1</p>
<p class="entry">Had meeting with client #Project2 & #Project3</p>

about 4 years ago · Juan Pablo Isaza Report

0

Maybe this solution will help ?

document.querySelectorAll("table td.entry").forEach((el) => {
  el.innerHTML = el.innerHTML.replace(/#([a-zA-Z0-9]+)/g, '<a href="search.php?`query=#$1">#$1</a>')
})
<table>
  <tbody>
    <td class="entry">Had meeting with client #Project1</td>
    <td class="entry">Had meeting with client #Project2</td>
    <td class="entry">Had meeting with client #Project3</td>
    <td class="entry">Had meeting with client #Project4</td>
    <td class="entry">Had meeting with client #Project5</td>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

try it:

const str= '<td class="entry">Had meeting with client #Project1</td>';

const match = str.match(/#([a-zA-Z0-9]+)/g);

var result = str.replace(match[0],`<a href="search.php?query=${match[0]}">${match[0]}</a>`);
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!