-1

i need to replace all [counter] occurences in a javascript variable. I have no idea why i don't get it working because when i use an online regexp tool like https://regex101.com/ , it matches the [counter]-occurences i want.

enter image description here

Example string to replace in:

<div id="ITEM-9" data-widget-id="9" data-widget-type="CounterWidget" data-counter="6" data-icon="fa fa-bell" data-filterid="2" data-hidewhenzero="1" data-countervalue="[counter]" class="cust-masonry-item col-xs-6 col-sm-4 col-md-3 col-lg-3 cust-masonry-item-height-1 ng-scope" style="position: absolute; left: 74.9035%; top: 0px;"><div class="cust-masonry-item-content"><div class="cust-dashboard-counter"><span class="cust-dashboard-counter-icon"><span class="fa fa-bell"></span></span><span class="cust-dashboard-counter-title cust-readable-text">API</span><span class="cust-dashboard-counter-count"><span class="cust-dashboard-counter-number">[counter]</span><span class="cust-dashboard-counter-subscript cust-readable-text">&nbsp;Log messages to read</span></span></div></div></div>

And i try to replace the [counter]-strings with the word 'replaced' like this:

var newHTML = element[0].outerHTML.toString().replace('/(\[counter\])/g', 'replaced');

Expected outcome:

<div id="ITEM-9" data-widget-id="9" data-widget-type="CounterWidget" data-counter="6" data-icon="fa fa-bell" data-filterid="2" data-hidewhenzero="1" data-countervalue="replaced" class="cust-masonry-item col-xs-6 col-sm-4 col-md-3 col-lg-3 cust-masonry-item-height-1 ng-scope" style="position: absolute; left: 74.9035%; top: 0px;"><div class="cust-masonry-item-content"><div class="cust-dashboard-counter"><span class="cust-dashboard-counter-icon"><span class="fa fa-bell"></span></span><span class="cust-dashboard-counter-title cust-readable-text">API</span><span class="cust-dashboard-counter-count"><span class="cust-dashboard-counter-number">replaced</span><span class="cust-dashboard-counter-subscript cust-readable-text">&nbsp;Log messages to read</span></span></div></div></div>

Yes, i am sure that the outerHTML contains the above string, however my words are not replaced.

I feel so dumb right now... Thanks for pointing me in the right direction.

Verthosa
  • 1,671
  • 1
  • 15
  • 37

1 Answers1

1

regex is not a string

newHTML = element[0].outerHTML.toString().replace(/(\[counter\])/g, "replaced");
Eduard Void
  • 2,646
  • 1
  • 13
  • 13
  • Hi thanks for your response, but i think the SO code tag removed my double "". It is var newHTML = element[0].outerHTML.toString().replace("/(\[counter\])/g", response.data); in my code – Verthosa Nov 21 '17 at 08:19
  • 1
    that quotes are the problem, skip them. regex is a special object not a string – Eduard Void Nov 21 '17 at 08:20
  • Oh damn, i feel even dumber now. Tnx a lot for your response, this fixed it indeed... – Verthosa Nov 21 '17 at 08:21
  • In js there are methods where the engine will interpret the string input as `regex`. So maybe your confusion come from there. – U Rogel Nov 21 '17 at 08:26