const input = document.querySelector('.word');
const divider = '-';
const secondDivider = '_';
const dividerRules = [
{ pattern: `[a-z0-9_-]`, replacement: divider },
{ pattern: `[\\${divider}\\${secondDivider}]+`, replacement: (match) => match[0] },
{ pattern: `^[\\${divider}\\${secondDivider}]+|[\\${divider}\\${secondDivider}]+$`, replacement: '' },
]
const valueReplacer = (input, rules) => {
rules.forEach(({ pattern, replacement }) => input.value = input.value.replace(new RegExp(pattern, 'gi'), replacement));
}
input.addEventListener('change', ({ target }) => {
valueReplacer(target, dividerRules);
});