I want to replace #### by ##-## (# := any digit symbol).
if (/^([0-9]{2})([0-9]{2})$/.test(str)) {
str = str.replace("/^([0-9]{2})([0-9]{2})$/", "$1-$2");
console.log(str);
}
In console I get #### (not ##-##).
What I'm doing wrong?
You need to pass a regex to the replace function not a string - in your case you are trying to replace the string literal /^([0-9]{2})([0-9]{2})$/
str = str.replace(/^([0-9]{2})([0-9]{2})$/, "$1-$2");
var str = '4455';
snippet.log('before: ' + str);
str = str.replace(/^([0-9]{2})([0-9]{2})$/, "$1-$2");
snippet.log('after: ' + str);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>