0

I want to know why this procedure doesn't replace words

I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope

var mySplitResult = Val.split(' ');   
for (var i = 0; i < mySplitResult.length; i++) {
  if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
    var start = mySplitResult[i].lastIndexOf(".") + 1;
    var end = mySplitResult[i].indexOf("}}");
    var result = mySplitResult[i].substring(start, end);
    for (var key in ticket.PNData) {
      if (key == result) {
        change.replace(mySplitResult[i], ticket.PNData[key]);
        alert(change)
      }
    }
  }
}  
Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
  • bcoz "{" is a special character in regular experssion; u need to [escape it](http://blog.simonwillison.net/post/57956816139/escape) –  Apr 09 '14 at 09:34

1 Answers1

1

In JavaScript strings are immutable which means you must assign the result to a variable.

mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);
axelduch
  • 10,769
  • 2
  • 31
  • 50