Javascript Regex to match only a single occurrence no more or less

Solution 1:

You can do this

/^[^-]+-[^-]+$/

^ depicts the start of the string

$ depicts the end of the string

[^-]+ matches 1 to many characters except -

Solution 2:

/^[^-]*-[^-]*$/

Beginning of string, any number of non-hyphens, a hyphen, any number of non-hyphens, end of string.