Regular Expression
-
Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs
-
Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs
Just use
?
operator Like this:^\d{3}-?\s?\d{3}-?\s?\d{4}$
You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.
-
Just use
?
operator Like this:^\d{3}-?\s?\d{3}-?\s?\d{4}$
You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.
Thanks a ton Firo, will give it a try... Morgs
-
Just use
?
operator Like this:^\d{3}-?\s?\d{3}-?\s?\d{4}$
You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.
I think that would match values like: 123456-7890 123456 7890 123456- 7890 123-4567890 123-456 7890 123-456- 7890 123 4567890 123 456-7890 123 456- 7890 123- 4567890 123- 456-7890 123- 456 7890 123- 456- 7890 Which are not in his spec.
-
Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs
If you want to ensure that the separators (if any) between the groups are the same, I think you need to use a backreference[^]. Something like this perhaps:
(?'Area'\d{3})(?(?=(\s|-))(?'Sep'(\s|-)))(?'Prefix'\d{3})(?(Sep)\k<Sep>)(?'Number'\d{4})
modified on Sunday, July 17, 2011 2:00 PM
-
Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs
I think this is what you need. ^\d{3}(-|\s)?\d{3}(-|\s)?\d{4}$
Simply Elegant Designs JimmyRopes Designs
Think inside the box! ProActive Secure Systems
I'm on-line therefore I am. JimmyRopes -
Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs
Have a look at this pattern: ^\d{3}[\s-]?\d{3}[\s-]?\d{4}$
modified on Tuesday, August 23, 2011 8:35 PM