// function hasSNILS(string) {
// // let result = 0
// const regexp1 = /\d{3}-\d{3}-\d{3} \d{2}/
// const result = string.match(regexp1)
// return result[0]
// }
 
// console.log(hasSNILS('234-600-142 22'))
// // console.log(hasSNILS('234-600 142 22'))
 
// function hasSNILS(string) {
// // let result = 0
// const regexp1 = /[0-9]/
// const result = string.match(regexp1)
// return result[0]
// }
 
// console.log(hasSNILS('234-600-142 22'))
// // console.log(hasSNILS('234-600 142 22'))
 
 
// function hideAllSNILS(string) {
// return string.replace(/\d{3}-\d{3}-\d{3}/g, "XXX-XXX-XXX" )
// }
 
// console.log(hideAllSNILS('234-600-142 222'))
 
// function formatSNILS(string) {
// // const result = /\d{3}-\d{3}-\d{3} /g
// return string.replace(/[^0-9] /g, "-")
// }
// // console.log(formatSNILS('48001443027', '480.014.430.27', '480--014--430 27'))
// console.log(formatSNILS('480.014.430.27'))
 
 
 
function formatSNILS(string) {
const digits = string.replace(/\D/g, '');
return digits.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3 $4");
}
 
console.log(formatSNILS('48001443027'));
console.log(formatSNILS('480.014.430.27'));
console.log(formatSNILS('480--014--430 27'));
Click Run to execute JavaScript.
Code runs in an isolated environment.