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?

2
  • 1
    Hope this 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 Commented Apr 13, 2017 at 6:59
  • Thank you, it's a useful thread too :) Commented Apr 13, 2017 at 7:09

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

5 Comments

This is exactly what I was looking for. Thank you for your working example and your help, I understand it well with the Regex explanation :)
No need using capturing groups when you need to refer to the whole match value, use .replace(/\w+:\w+/gi, 'el.$&'). If you use \w*:\w*, you will add el. before any :.
@WiktorStribiżew Thanks , i have updated the answer and the links.
I checked it as the best answer because It matches the correct string without changing it, and it's also working with alphanumeric characters :)
@NicolasLeucci We always have room for improvement, Happy Coding :)
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.

1 Comment

Thank you ! I actually need to append all iteration in my string
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.

1 Comment

Explain what does it mean $1 and $2
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.