0

All I need to do here is to add a variable before each specific string.

Example:

var exampleString = "blabla:test abcde 123test:123";
var formattedString = "el.blabla:test abcde el.123test:123";

As you can see, when I have something like "XXX:XXX", I need to add a variable before it.

I have the Regex to find "XXX:"

var regex = new RegExp(/\w+([aA-zZ]:)/g)

But when I try to replace it, it replaces all instead of adding the variable "el."

var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(new RegExp(/\w+([aA-zZ]:)/g), 'el.');
// formattedString is now "el.test abcde el.123"
// Instead of "el.blabla:test abcde el.123test:123"

Could anyone makes this work ? Thanks :)

Source: Javascript Regex: How to put a variable inside a regular expression?

Community
  • 1
  • 1
Nicolas Leucci
  • 2,829
  • 1
  • 13
  • 12
  • 1
    Hope [this](http://stackoverflow.com/questions/3598042/how-can-i-replace-a-regex-substring-match-in-javascript) may help you. Actually the logic is that you can use `()` to group thee desired substring. And the in replace string you can use "$1" to use the first matched group – Abhisek Malakar Apr 13 '17 at 06:59
  • Thank you, it's a useful thread too :) – Nicolas Leucci Apr 13 '17 at 07:09

4 Answers4

3
var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/\w*:\w*/gi, 'el.$&');
console.log(formattedString);

Regex use and Explanation Here https://regex101.com/r/U2KeXi/3

Sample Fiddle here https://jsfiddle.net/a8wyLb0g/2/

1

You need to use ^ to match only at the beginning. And remove the g modifier, since you only want to replace once, not every time.

There's also no reason to use new RegExp(), just use a RegExp literal.

In the replacement string, you need to use $& to copy the original string into the replacement.

var exampleString = "blabla:test abcde 123test:123";
var formattedString = exampleString.replace(/^\w+[a-z]:/i, 'el.$&');
console.log(formattedString);

Also, the proper way to match all letters in either case is with [A-Za-z], not [aA-zZ], or use the i modifier to make the regexp case-insensitive. Your regexp matches all characters in the range A-z, which includes lots of punctuation characters that are between the uppercase letters and lowercase letters in the ASCII code.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Just use this

exampleString.replace(/(\w*):(\w*)/gi, 'el.$1:$2');

REGEXP explanation :

capturing group (\w*) is for capturing any alphabets in any number of occurance, $1 and $2 specifies the first and second capturing group.

binariedMe
  • 4,309
  • 1
  • 18
  • 34
0

You should use a function like insertAt instead replace, see following example:

String.prototype.insertAt=function(index, string) { 
  return this.substr(0, index) + string + this.substr(index);
}

var exampleString = "blabla:test abcde 123test:123";
var regex = new RegExp(/\w+([aA-zZ]:)/g)
var formattedString = exampleString;

while ( (result = regex.exec(exampleString)) ) {
    formattedString = formattedString.insertAt(result.index, "el.");
}

console.log(formattedString);

I hope it helps you, bye.

Alessandro
  • 4,382
  • 8
  • 36
  • 70