ab_732
ab_732

Reputation: 3897

Global replace in a regexp expression

I'm trying to write a Javascript regexp replace function that allows me to replace every instance of some tags delimited by squared brackets with an arbitrary string.

function TestTags() {
    var url = "test1=[A1]&test2=[A1]";
    result = url.replace('\[A1\]', 'test');
    console.log("result = " + result);
}

This works fine only for the first occurrence:

result = test1=test&test2=[A1]

I know it's pretty silly but I haven't be able to set the 'g' parameter to make it global. Can someone help me?

Thank you.

Upvotes: 0

Views: 80

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78580

just add a g to it and use the // syntax:

url.replace(/\[A1\]/g, 'test');

Upvotes: 2

Related Questions