Using javascript replace to replace numbers in a string?
10,061
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
Comments
-
Rami Dabain almost 2 years
I have a string
var x='abcdefg1234abcdefg';
I want to replace
1234
with555
using thex.replace()
function likex=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
andsb_height
.