Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript replace to replace numbers in a string?

I have a string

var x='abcdefg1234abcdefg';

I want to replace 1234 with 555 using the x.replace() function like

x=x.replace(stuff here)

I tried to pass '1234','555' as the parameters but it's not working.

Any clues? thanks

Edit: The string is in a hidden <input> field. The value of it is:

<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="400" height="385">
 <param name="movie" value="player.swf" /> <param name="allowfullscreen" value="true" />
 <param name="allowscriptaccess" value="always" />
 <param name="flashvars" value="provider=http&file=files/videos/10.file" />
 <embed type="application/x-shockwave-flash" src="player.swf" width="400" height="385" allowscriptaccess="always" allowfullscreen="true" flashvars="provider=http&file=files/videos/10.file&image=preview.jpg"/>
</object>

I want to replace the width value and the height value to values stored in sb_width and sb_height.

like image 281
Rami Dabain Avatar asked Jun 20 '26 10:06

Rami Dabain


1 Answers

What you've described should have worked:

var x = 'abcdefg1234abcdefg';
x = x.replace('1234', '555');
alert(x); // alerts abcdefg555abcdefg

But note that replace when given a string will only replace the first one. To replace more, use a regex with the g flag (for "global"):

var x = 'abcdefg1234abcdefg1234xxx';
x = x.replace(/1234/g, '555');
alert(x); // alerts abcdefg555abcdefg555xxx

Live example

like image 118
T.J. Crowder Avatar answered Jun 21 '26 22:06

T.J. Crowder