function timeFormat(str) { return str.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3 ") } var str = '20180210'; var time = timeFormat(str); console.log(time);