Вы можете использовать метод JavaScript test () следующим образом:
const string1 = "abra-ibra_cadabra.2";
const string2 = "!abra-ibra";
const string3 = "(abra)_ibra";
let patt = /^(\w|\.|-)+$/;
console.log('string1:', patt.test(string1));
console.log('string2:', patt.test(string2));
console.log('string3:', patt.test(string3));
Объяснение:
^ : asserts position at start of the string,
$ : asserts position at the end of the string,
+ : Quantifier — Matches between one and unlimited times,
Группа захвата:
Между скобками, все альтернативы отделяются "|" (или) символ:
(\w|\.|-)
1-й вариант:
\w : matches any word character (equal to [a-zA-Z0-9_])
2-й вариант:
\. : matches the character . literally (case sensitive)
3-й вариант:
- : matches the character - literally (case sensitive)