2

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! :)

4
  • 1
    What language are you using? JavaScript, PHP,... ? Commented Jan 21, 2013 at 9:10
  • In the end its JavaScript. Commented Jan 21, 2013 at 9:10
  • 2
    Have you used the /g modifier? Commented Jan 21, 2013 at 9:12
  • look here:stackoverflow.com/questions/4285472/multiple-regex-replace Commented Jan 21, 2013 at 9:14

1 Answer 1

8

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.