0

Just wandering will it be possible to partially string replace in jquery?

I have try to using the following code, but this is not seem working for me:

var test = "testing_supplyAddress_001";
test.replace('supplyAddress', 'billingAddress');

I tried to replace only supplyAddress to billingAddress so the output will be testing_billingAddress _001

2
  • This has nothing to do with jQuery. That's pure JavaScript. Commented Mar 30, 2011 at 23:53
  • This is not possible with jQuery as it does not provide any string manipulation functions. But you can use basic JavaScript like you already do. Commented Mar 30, 2011 at 23:55

3 Answers 3

2

JavaScript strings are static and thus .replace() does not actually modify the string. You'll need to assign the value returned by the .replace() function back to the variable:

var test = "testing_supplyAddress_001";
test = test.replace('supplyAddress', 'billingAddress');

Here's a demo showing this in action ->

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

Comments

2

It works fine. It doesn't replace it in place though - the replace() method returns a new string.

var test = "testing_supplyAddress_001";
var newTest = test.replace('supplyAddress', 'billingAddress');
alert(newTest);

Comments

0

This is just plain old javascript - but will work with jQuery too.

var test = "testing_supplyAddress_001".replace('supplyAddress', 'billingAddress');

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.