user1856596
user1856596

Reputation: 7233

Replace a string several times with a regex (JavaScript)?

I have this string:

<li><a class="amshopby-attr-selected" href="">1</a> (320)</li>
<li><a class="amshopby-attr" href="">2</a> (2)</li>
<li><a class="amshopby-attr" href="">3</a> (1)</li>

I want to replace the a class="amshopby-attr-selected" href with something else in the whole string (each line). I tried using "<a class="amshopby-attr-selected" href" as regex, but this only finds and replaces it once. How can I replace all at once?

Thanks! :)

Upvotes: 2

Views: 982

Answers (1)

Mattias Buelens
Mattias Buelens

Reputation: 20159

You'll need to specify the g modifier to indicate a global search. Otherwise, the regular expression will only match once.

var replaced = original.replace(/<a class="amshopby-attr-selected" href"/g, substitute);

Upvotes: 8

Related Questions