Jin Yong
Jin Yong

Reputation: 43778

Partial string replace in jquery

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

Upvotes: 0

Views: 927

Answers (3)

Billy Moon
Billy Moon

Reputation: 58531

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

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

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318518

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);

Upvotes: 2

Ender
Ender

Reputation: 15221

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 ->

Upvotes: 2

Related Questions