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

96
Views
How to replace particular patterns of text with HTML tags on front-end using JavaScript?

There is a field in the CMS that does not allow HTML, but now we have to do some complex CSS styling to change the layout.

My idea is to replace some particular patterns of text with HTML tags.

For example, to change:

<div class="wrap">
  <figcaption>###2022-10-01###Opening Ceremony</figcaption>
  ...
  <figcaption>###2022-10-02###Welcoming Speech</figcaption>
  ...
  <figcaption>###2022-10-03###Race Day</figcaption>
</div>

into:

<div class="wrap">
  <figcaption><span class="date">2022-10-01</span>Opening Ceremony</figcaption>
  ...
  <figcaption><span class="date">2022-10-02</span>Welcoming Speech</figcaption>
  ...
  <figcaption><span class="date">2022-10-03</span>Race Day</figcaption>
</div>

Note: ###2022-10-02###Welcoming Speech is what I can put into the field.
So it is not a must to be ###. It can be other text, symbols, or patterns.

Shall we use htmlContent.replace? And I have to target all matched patterns on the web page. I am not sure how to do it correctly. ๐Ÿ™๐Ÿป

Trying on CodePen: https://codepen.io/pen/qBYXxbm

almost 4 years ago ยท Santiago Trujillo
2 answers
Answer question

0

Use a regular expression replacement.

htmlContent = htmlContent.replace(/###(.*?)###/g, '<span class="date">$1</span>');

(.*?) is a capture group that captures everything between the pair of ###, and $1 in the replacement string copies that this captured.

let htmlContent = `<figcaption>###2022-10-01###Opening Ceremony</figcaption>
...
<figcaption>###2022-10-02###Welcoming Speech</figcaption>
...
<figcaption>###2022-10-03###Race Day</figcaption>`;

htmlContent = htmlContent.replace(/###(.*?)###/g, '<span class="date">$1</span>');
console.log(htmlContent);

almost 4 years ago ยท Santiago Trujillo Report

0

Details are commented in example

// Collect all <figcaption> into a NodeList
document.querySelectorAll("figcaption")
  // For each <figcaption>...
  .forEach(cap => {
    // ...split it's text at "###" into an array
    const text = cap.textContent.split('###')
      // ...filter any empty parts of the array
      .filter(t => t);
    //console.log(text);
    /*
    Render the template literal within the <figcaption>
    interpolate the content of array text
    */
    cap.innerHTML = `<span class="date">${text[0]}</span> ${text[1]}`;
  });
section,
figure {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}

.date {
  color: red
}
<section>
  <figure>
    <img src="https://s.abcnews.com/images/International/olympics-fireworks4-gty-ml-220204_1643987789890_hpEmbed_3x2_992.jpg" width="240">
    <figcaption>###2022-10-01###Opening Ceremony</figcaption>
  </figure>
  <figure>
    <img src="https://gupshups.org/wp-content/uploads/2019/11/Welcome-speech-for-the-seminar.jpg" width="240">
    <figcaption>###2022-10-02###Welcoming Speech</figcaption>
  </figure>
  <figure>
    <img src="https://cdn.quotesgram.com/img/70/24/616754473-2c33e6276057e5c799641af64e446ccb.jpg" width="240">
    <figcaption>###2022-10-03###Race Day</figcaption>
  </figure>
</section>

almost 4 years ago ยท Santiago Trujillo 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!