0

I need to match the domain name form the string .With three different pattern.

var str=" with http match http://www.some.com and normal website type some.com and with www.some.com  ";
var url = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/g;
console.log(str.match(url))

Above snippet match only with http://www.some.com.

But i need to match with three types.

  1. http://www.some.com
  2. www.some.com
  3. some.com

Help me find the result.I am not very well in regex.I get this regex pattern from stack overflow. But the not satisfied with three conditions.

SamWhan
  • 8,038
  • 1
  • 14
  • 42
prasanth
  • 19,775
  • 3
  • 25
  • 48

4 Answers4

2

Use

(?:(http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&;:\/~+#-]*[\w@?^=%&;\/~+#-])?

this just makes the http/ftp/... optional (without capture ?:)

see example here: demo

or as graphic here

Fallenhero
  • 1,515
  • 1
  • 6
  • 17
1

As said before, you can make some parts of the regex optional with ()?, for example : (http:\/\/)?(www\.)?(some\.com). So with your code, maybe something like this :

var str=" with http match http://www.some.com and normal website type some.com and with www.some.com but matched http://----.-.-.-. and now will match ----.-.-.-.";
 var url = /((http|ftp|https):\/\/)?[\w-]*(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/g;
 console.log(str.match(url))

But the regex you provide matches strings like "http://----.-.-.-.", and with this modification it will now match ----.-.-.-. for example, which is not what you want. If you are really trying to match a URI, you need to use a different regex.

Here are some resources to help you improve this answer : https://regex.wtf/url-matching-regex-javascript/

see What is the best regular expression to check if a string is a valid URL? where the RFC is quoted : http://www.faqs.org/rfcs/rfc3987.html

Note : they all seem to match "http://----.-.-.-.", so maybe your regex is not much worse.

Community
  • 1
  • 1
smmilut
  • 11
  • 1
0

To match Unicode characters, you should use this one:

(ftp:\/\/|www\.|https?:\/\/)?[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)

Demo here

Duc Filan
  • 5,158
  • 1
  • 18
  • 24
-1

var pattern = /((https|http|ftp){1}:\/\/)?(www\.)?\w+\.\w{2,4}/ig;
var test = ['http://www.some.com/NotRelevant',
  'https://www.some.com/NotRelevant',
  ':/www.some.com/NotRelevant',
  'www.some.com/NotRelevant',
  'some.com/NotRelevant'
];
for (var t = 0; t < test.length; t++) {
  console.log(test[t], test[t].match(pattern));
}
Emil S. Jørgensen
  • 5,684
  • 1
  • 9
  • 22