First off, make up your mind what language you are using: a JS solution will not be identical to a Java or C# solution.
Assuming C#, your expression works fine:
string pattern1 = @"\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}";
Regex reg = new Regex(pattern1);
if (reg.IsMatch("123456789")) Console.WriteLine("9 OK");
if (reg.IsMatch("123-45-6789")) Console.WriteLine("3-2-4 OK");
if (reg.IsMatch("123-456789")) Console.WriteLine("3-6 OK");
if (reg.IsMatch("12345-6789")) Console.WriteLine("5-4 OK");
Gives:
9 OK
3-2-4 OK
3-6 OK
5-4 OK
The only difference I would make is to add start and end of line characters:
string pattern1 = @"^\d{9}|\d{3}-\d{2}-\d{4}|\d{3}-\d{6}|\d{5}-\d{4}$";
Regex reg = new Regex(pattern1);
So what's the problem with it?