vijar
vijar

Reputation: 781

How to replace string in javascript?

I have a string which is like

var test="clientNumber=123,TestSubject=Tom";

I need to replace Tom with Mary. Tom is entered by the user and could be anything but i need to make it Mary while processing . Also clientNumber & TestSubject could be in any order in the string.

Thanks

Upvotes: 0

Views: 60

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382150

You seem to want to use a regular expression, for example

test = test.replace(/(TestSubject=)[^,]*/, '$1Mary')

The code I give replaces everything between "TestSubject=" and the end of string or the next comma.

This changes

"clientNumber=123,TestSubject=Tom" into "clientNumber=123,TestSubject=Mary"

and

"TestSubject=Tom,clientNumber=123" into "TestSubject=Mary,clientNumber=123"

Upvotes: 5

Related Questions