使用javascript替换来替换字符串中的数字? [英] Using javascript replace to replace numbers in a string?
问题描述
我有一个字符串
var x='abcdefg1234abcdefg';
我想用<$替换 1234
c $ c> 555 使用 x.replace()
函数,如
I want to replace 1234
with 555
using the x.replace()
function like
x=x.replace(stuff here)
我试图通过'1234','555'
作为参数,但它不起作用。
I tried to pass '1234','555'
as the parameters but it's not working.
任何线索?谢谢
编辑:
该字符串位于隐藏的< input>
字段中。它的价值是:
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>
我想将宽度值和高度值替换为存储在中的值sb_width
和 sb_height
。
I want to replace the width value and the height value to values stored in sb_width
and sb_height
.
推荐答案
什么你所描述的应该有效:
What you've described should have worked:
var x = 'abcdefg1234abcdefg';
x = x.replace('1234', '555');
alert(x); // alerts abcdefg555abcdefg
但请注意替换
给定一个字符串只会替换第一个字符串。要替换更多,请使用带有 g
标记的正则表达式(对于全局):
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
这篇关于使用javascript替换来替换字符串中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!