'JavaScript Regex find and replace multiple word with multiple word without using multiple replace function

I am using Angular7 and I want to change url defined in the environment file without concatenation.

So I have a string in my component like this.

"upload/document/:orgId/products/:productId"

I want to replace it with 2 ids using regex-only so I can get output like this. I do not want to use multiple replace calls.

 "/upload/document/101/products/99101"

Thanks in advance!.



Solution 1:[1]

var template = "upload/document/:orgId/products/:productId";

var result = template.replace(/:orgId\b/, '101').replace(/:productId\b/, '99101');

console.log(result);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pedro Lima