0

I have a simple javascript code as follows

var url = '@Url.Action("Action", "Controller", new { id = "_id_", id2 = "_id2_", id3 = "_id3_", id4 = "_id4", ... })';

used in the ajax calls where idxx is replaced with other values However the var url is generated with "...&id2=id2&..." etc. and i just learned that javascript string.replace() works only on the first occurance

wondering if there's another way to code the var url rather than replace multiple times ?

the solution has to be universally executable i.e. run in older browsers as well

Kumar
  • 10,997
  • 13
  • 84
  • 134
  • this is within the past year, the solution has to work everywhere, updated the question – Kumar Mar 11 '21 at 04:15
  • Please check this answer from same question https://stackoverflow.com/a/1144788/9695286. It has used `RegExp` which will work for older browsers as well. – Karan Mar 11 '21 at 04:17
  • that should work, does the & need any special handling for regex ? – Kumar Mar 11 '21 at 04:24

1 Answers1

1

You can use Replace all which will replace all occurrences

var test = 'id=_id&id=_id&id=test';
var replacedStr = test.replaceAll('_id','teststr');

console.log(replacedStr);

.

Rutuja
  • 460
  • 2
  • 12