Regular expression '<>' validation

I am currently working on creating a regular expression that will accept URLs and hostnames.

So, the following should be accepted:

 google google.com www.google.com http://google.com http://www.google.com 

However, the following should not be taken:

 <xml> <html> 

The expression I have received so far:

 ([a-zA-Z0-9])|((http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?) 

However, this part of the expression: ([a-zA-Z0-9])
matches <xml> and <html>

Are there any suggestions as to what I am missing here?

+4
source share
1 answer

You need to add the expression start ( ^ ) and end ( $ ) to the expression to make sure that only the pattern you specify is allowed:

 ^([a-zA-Z0-9]+)|((https?://)?([\w-]+\.)+[\w-]+(/[-\w ;,./?%&=]*)?)$ 
+5
source

Source: https://habr.com/ru/post/1491473/


All Articles