localZero localZero - 2 years ago 93
Javascript Question

Replace a number in a string

Ok so i have the following string,

var str = 'some text {Foo} some more text {9}';

Now i know how to replace specific characters/words like,

var newStr = str.replace(/{Foo}/g, 'bar');

But how would i replace that {9} when the number can be any number?

Answer Source

You could use the meta sequence for a digit \d.

var str = 'some text {Foo} some more text {9}',
    newStr = str.replace(/{\d+}/g, 'bar');


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download