Home > OS >  Issue with `` within .replace() tags
Issue with `` within .replace() tags

Time:07-06

How can I edit my current code to work with `` within replace. As of current because it's inside a loop .each it will find the correct word but then add <span></span> around that word like 6 times. I want it to only add it once, so I read you can use /g for that, however using `` I think is causing my issue, however I don't know how to incorporate both?

Here is my JS:

$("#pstad-descrptn-mirror div").each(function () {
   let get_pstad_desc_div = $(this).text();
   let get_pstad_desc_STORE = get_pstad_desc_div
       .split(", and comes with")[0]
       .split("located in ")[1];                

   $(this).html(function () {
      return $(this)
        .html()
        .replace(
           `/(${get_pstad_desc_STORE})/g`,
           `<span>${get_pstad_desc_STORE}</span>`
        );
   });
}) 

I'm open to a javascript or jquery solution.

Many thanks

  • Related