0

I'm trying to replace portions of a textarea text by regex.

propertiesText is the full text received from the textarea, and the replacement code is as follows:

var propertyName = inputs[i].name;
var propertyValue = inputs[i].value;

//#regexExpression = #propertyName=.* [example: /#EPCDatabase\/EPCdatabase.param\/epc.db.user=.*$/gim]//
var regexExpression = new RegExp(propertyName + "=.*$","gim");
//#newString = propertyName=propertyValue [example: EPCDatabase\/EPCdatabase.param\/epc.db.user=ddddd]//
var newString = propertyName.substring(1) + "=" + propertyValue;
console.log("Regex Expression: " + regexExpression);
console.log("Replace String: " + newString);

propertiesText.replace(regexExpression, newString);
console.log(propertiesText);
return;

In the console, I'm getting the following regex expression:

Console Regex

But the text is not being replaced in the original propertiesText:

Properties String after replace

I've tried checking my regex and you can see it is matching.

I've also tried isolating the replacement code part with the same regex which is outputted, and as you can see again, its working.

What am I missing in my code ?

Alon Adler
  • 3,984
  • 4
  • 32
  • 44

1 Answers1

3

String Replace doesnt acutally update the current string, but simply returns a new string with the replace applied. So to fix your code update to

propertiesText = propertiesText.replace(regexExpression, newString);

See http://www.w3schools.com/jsref/jsref_replace.asp

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

Cleared
  • 2,490
  • 18
  • 35
  • Damn me (blushing), I was suspecting the replacement inst actually replacing, but i was too blind thinking the Regex was the issue. Only thing left is to say a big **Thank you**. – Alon Adler Nov 16 '16 at 13:11