0

I need a regular expression that validates all URL that starts with either (a1 can be caps or small). I know how to validate a URL but not sure how to add the constant to it. I am new to reg Ex.

Input:

www.a1.  
http:// www.a1.  
https:// www.a1.  
http:// a1.  
https:// a1.  
a1.  

and can have any number of trailing subdirectory. Ive written a sample code but that doesnt solve this. Please help. My code :

^(http:\/\/|https:\/\/)?(www.)?(a1|A1).([a-zA-Z0-9]+).?[a-zAZ0-9]*.[a-z]{3}.?([a-z]+)?$

It fails for my inputs.

  • 1
    What do you mean nothing works? What have you tested your regex with and in what way does it not work? We would be better able to help you if you include example input and desired output. – KernelPanic Mar 06 '17 at 06:19
  • yes i have. example inputs are https://a1.example.com/blog-test , a1.example.com , and it fails. Ive also tried other regular expressions such as ^(http[s]?:\\/\\/(www\\.)?|www\\.){1}(a1|A1).([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)? . Please help – tumblerwolverine Mar 06 '17 at 06:27
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Jun 30 '17 at 06:12
  • Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Jun 30 '17 at 06:12
  • Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) –  Jun 30 '17 at 06:13
  • @JarrodRoberson It is a valid question as i needed to know if the URL starts with the value 'xx' or not. So its not a duplicate. Please verify your facts before your post. – tumblerwolverine Jun 30 '17 at 06:18

1 Answers1

0

You can use the following:

/^(https?:\/\/)?(www\.)?a1.*/i.test('www.a1.') => true

Explanation:

  • Start with http:// or https:// (optional)
  • Continue with www. (optional)
  • Then you need an a1.
  • Then anything you want.

The /i means to ignore case.

Fiddle at: http://www.regexpal.com/?fam=97095

EDIT

The regex below doesn't check to ensure it's actually a valid URL, just that it matches the rules above. Checking to see whether a string is a valid URL can be tricky, see What is the best regular expression to check if a string is a valid URL? for more information on that.

Community
  • 1
  • 1
Scovetta
  • 3,112
  • 1
  • 14
  • 13