TheKingPinMirza
TheKingPinMirza

Reputation: 8912

how to replace part of part of string in javascript

I have two GUIDs. I am looking for to replace c013d94e from 1st guid with cd11d94e of second guid in Javascipt.

I checked javascript replace() method but not sure how i can use it with my specific case.

c013d94e-3210-e511-82ec-303a64efb676 - 1st Guid

cd11d94e-3210-e511-82ec-303a64efb676 - 2nd Guid

Following is my code where i am trying to do it

for(var i=0; i < response[1].length;i++)
    angular.forEach($scope.studentPermissions[i][0].Children, function (subject) {
    string 1stGuid= response[1].data[i].Id;   // it contains cd11d94e-3210-e511-82ec-303a64efb676
    subject.Id = // it contains c013d94e-3210-e511-82ec-303a64efb676

    });

Upvotes: 0

Views: 51

Answers (2)

user3117575
user3117575

Reputation:

@Jamen. Yes the other part of 1st string will always be same. How can i use concatenate?

You don't even need to use replace then? Just make a brand new string:

var guid = "cd11d94e-3210-e511-82ec-303a64efb676";

But, to actually answer the question in the title:

var input = "c013d94e-3210-e511-82ec-303a64efb676";
var output = input.replace("c013d94e", "cd11d94e");

console.log(output); // cd11d94e-3210-e511-82ec-303a64efb676

But like I said, in your situation this shouldn't be necessary, based on the quote.

Upvotes: 0

Alex McMillan
Alex McMillan

Reputation: 17942

replace takes 2 parameters, the first is the string to search for and the second is the replacement string. It doesn't modify the original string, it simply returns a new string with the value replaced.

You can perform your replace like this:

var guid = 'c013d94e-3210-e511-82ec-303a64efb676';
guid = guid.replace('c013d94e', 'cd11d94e');

console.log(guid); // 'cd11d94e-3210-e511-82ec-303a64efb676'

Upvotes: 1

Related Questions