My task here is to prepend a variable before each specific string in the given text.
For example:
var exampleString = "blabla:test abcde 123test:123";
var formattedString = "el.blabla:test abcde el.123test:123";
In this case, whenever there is a pattern like "XXX:XXX", I am required to add a variable before it.
I already have a regex pattern to identify "XXX:"
var regex = new RegExp(/\w+([aA-zZ]:)/g)
However, when attempting to replace it, all occurrences are replaced instead of just adding the variable "el."
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(new RegExp(/\w+([aA-zZ]:)/g), 'el.');
// The result should be "el.blabla:test abcde el.123test:123"
// But it returns "el.test abcde el.123"
Can someone help me make this work correctly? Thank you :)
Source: Javascript Regex: How to put a variable inside a regular expression?