0

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

3
  • Do you specifically need to replace TestSubject=Tom with TestSubject=Mary, or do you need to replace any occurrence of Tom with Mary? Commented Jan 2, 2014 at 20:04
  • just hard code it, and forget about it. Commented Jan 2, 2014 at 20:04
  • I would split the string by the , so that there is an array. Find out which one contains TestSubject and use a regex that find the string that matches that and replace it with TestSubject=Mary Commented Jan 2, 2014 at 20:09

1 Answer 1

5

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"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.