I have an input control, user can enter only numeric values or hyphen. When everything is done, I should transform entered value from input into a valid fips code (it looks like 'xx-xxx'). What is the most efficient way to do that? From the first sight it seems basic for loop can do that. Are there more advanced ways like RegEx for this kind of transformations? Thanks!
const someUserInputValueBeforeOnBlur = '12313213-12312313'
let newString = ''
for (let i = 0; i < string.length; i++) {
if (i === 2) {
newString += '-'
}
if (i === 5) {
break
}
newString += string[i]
} //expected output of newString: 12-313