How to get the starting website name with regex? | SoloLearn: Learn to code for FREE!

+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.

5/31/2020 8:42:16 PM

Hassan Raza

4 Answers

New Answer

+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.

+1

How about that: var linkurl = link.match(/\bhttps:\/\/www.\w+.\w+/);

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);

0

I am saving the complete website name in a variable and then trying to seperate the start of a website with regex