+ 2

How to get the starting website name with regex?

var link = "https://www.google.com/search?q=backslash&oq=ba&aqs=chrome.0.69i59l3j69i57j69i60l4.502j0j7&sourceid=chrome&ie=UTF-8"; var linkurl = link.match(/[a-zA-Z0-9:]{5,6}//[.a-zA-Z0-9]{0,100}/g); console.log(linkurl); I want to get the https://www.google.com /[a-zA-Z0-9:]{5,6}//[.a-zA-Z0-9]{0,100}/g it's working fine in regex testing website but when i try it in browser console it showing error because of . I think.

31st May 2020, 8:42 PM
Hassan Raza
Hassan Raza - avatar
4 odpowiedzi
+ 2
This is rather harder than your other query. Sure, your pattern should match the website (although I would be inclined to remove the g at the end unless the link isn't slways at the start of the string), but it will also match a lot of other things too. Also, whether it's suitable will depend on whether you'll be trying to match other formats of website too (i.e. sites with hyphens, ftp, other variations I can't think of now) or just google each time. You could try: /[hpt]{4}s?:\/\/[^/]+/g Your issue above is that you will need to escape the "/"s.
31st May 2020, 9:34 PM
Russ
Russ - avatar
+ 1
How about that: var linkurl = link.match(/\bhttps:\/\/www.\w+.\w+/);
31st May 2020, 10:06 PM
JaScript
JaScript - avatar
0
I tried this in the browser console when it showed error: var link = "https://www.google.com/search?q=backslash&oq=ba&aqs=chrome.0.69i59l3j69i57j69i60l4.502j0j7&sourceid=chrome&ie=UTF-8"; var linkurl = link.match(/[a-zA-Z0-9:]{5,6}//[.a-zA-Z0-9]{0,100}/g); console.log(linkurl);
31st May 2020, 9:23 PM
Hassan Raza
Hassan Raza - avatar
0
I am saving the complete website name in a variable and then trying to seperate the start of a website with regex
31st May 2020, 9:38 PM
Hassan Raza
Hassan Raza - avatar