Is there a special reason you are preceding the number with a backslash? If your intention is to match against the string "{5}", "{" and "}" are the special characters that should be escaped, not the character "5" itself!
According to MDN:
A backslash that precedes a non-special character indicates that the next character is
special and is not to be interpreted literally. For example, a 'b' without a preceding '\' generally matches lowercase 'b's wherever they occur. But a '\b' by itself doesn't match any character; it forms the special word boundary character.
The following code would work:
var str = "foo{5}bar{5}";
var newStr = str.replace(/\{5\}/g, "_test_");
console.log(newStr); // logs "foo_test_bar_test_"