使用javascript替换替换字符串中的数字?
我有一根绳子使用javascript替换替换字符串中的数字?,javascript,replace,numbers,Javascript,Replace,Numbers,我有一根绳子 var x='abcdefg1234abcdefg'; 我想使用x.replace()函数将1234替换为555 x=x.replace(stuff here) 我试图将'1234'、'555'作为参数传递,但不起作用 有什么线索吗?谢谢 编辑: 字符串位于隐藏的字段中。它的价值是: <object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="4
var x='abcdefg1234abcdefg';
我想使用x.replace()
函数将1234
替换为555
x=x.replace(stuff here)
我试图将'1234'、'555'
作为参数传递,但不起作用
有什么线索吗?谢谢
编辑:
字符串位于隐藏的
字段中。它的价值是:
<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
中的值,您所描述的应该是有效的:
var x = 'abcdefg1234abcdefg';
x = x.replace('1234', '555');
alert(x); // alerts abcdefg555abcdefg
但是请注意,replace
当给定一个字符串时,它只会替换第一个字符串。要替换更多,请使用带有g
标志(表示“全局”)的正则表达式:
以下代码有效,请立即尝试:
var bla='dfasfdas123dfasfas';
alert(bla.replace('123','555'));
试过用firebug或其他东西调试吗?给我们您正在尝试的确切代码片段好的,这根本不是字符串替换。我希望您使用jQuery
$("$player").attr("width", sb_width);
$("$player").attr("height", sb_height);
$("$player").children("embed").attr("width", sb_width);
$("$player").children("embed").attr("height", sb_height);
字符串替换不是这里的方法。您需要对初始值进行硬编码,可能会替换一个意外的类似字符串,该代码并不能真正传达您的意图(更改高度/宽度,而不是操纵字符串)您能准确地发布您尝试过的内容吗?是的,我要的是正则表达式,我的值分布在整个字符串上……谢谢,它起到了作用:)。我只是在双引号ex“/1234/i”中使用正则表达式,并使用i而不是G。。。谢谢again@Ronan:很高兴这有帮助,但不需要双引号,文字正则表达式很好(如果有的话,首选)。
$("$player").attr("width", sb_width);
$("$player").attr("height", sb_height);
$("$player").children("embed").attr("width", sb_width);
$("$player").children("embed").attr("height", sb_height);