Using javascript replace to replace numbers in a string?

亡梦爱人 提交于 2019-12-06 12:10:00

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

following code works, tried now:

var bla='dfasfdas123dfasfas';
alert(bla.replace('123','555'));

Tried debugging in firebug or sth? Give us the exact code snippet you are trying

Ok, this isn't string replacement at all. You use jQuery I hope?

$("$player").attr("width", sb_width);
$("$player").attr("height", sb_height);

$("$player").children("embed").attr("width", sb_width);
$("$player").children("embed").attr("height", sb_height);

String replacement is not the way to go here. You'd need to hardcode the initial values, might replace an unintended similar string, the code doesn't really communicate your intention (to change height/width, not to manipulate strings)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!