window.onload = function() { var replaced_element = document.getElementsByClassName("translate-price"); for (var i = 0; i < replaced_element.length; i++) { replaced_element[i].innerHTML = replaced_element[i].innerHTML.replace(new RegExp("[0-9]", 'g'), "<span>Здесь должно быть найденное число</span>"); } } 

It is necessary to add the opening span tag to the found numbers and the closing span tag after the number. It is necessary to somehow substitute the found number, but I can not understand how. I would be grateful for any help)

    2 answers 2

    In the replaceable line, you can use the link $& . Then this link will be replaced with the found string.

     window.onload = function() { var replaced_element = document.getElementsByClassName("translate-price"); for (var i = 0; i < replaced_element.length; i++) { replaced_element[i].innerHTML = replaced_element[i].innerHTML.replace(/\d+/g, "<span>$&</span>"); } } 
     span { color: blue; } 
     <div class="translate-price">Цена: 500USD</div> 

       var el = document.querySelector("main") el.innerHTML = el.innerHTML.replace(/-?\d+/g, "<span>$&</span>") 
       span { color: red } 
       <main>Необходимо ко -1 всем найденным числам 789 добавить перед числом -789 открывающий тег span и после числа закрывающий тег span. Нужно как-то 456 подставить в замену найденное число, но не могу понять 99645646 5456 как. Буду благодарен 4564564 за любую помощь)</main>