Extract domain from a string using Javascript?

javascript get domain from string
javascript get domain name without subdomain
regex extract domain from url
javascript get subdomain from url
create domain name javascript
javascript get hostname from url
javascript get top level domain
javascript get url path

I have an string which include many website url but i want to extract the only url that is out side these bracket [ ].

Can someone correct this ?

Note : Output Must be www.google.com and it not necessary that domain name outside [ ] will come at the end of string.

var str = '[[www.abc.com/corporate/partner/just-a-test]]acdascvdvsa.1563e24e32e42|[[www.abc.com/corporate/partner/just-a-test]]1563e24e32e42.1563e24e32e42|[[www.abc.com/instruments/infrared-guided-measurement/]]www.google.com&1566805689640.1566806059701.3';

// String can include https and instead of .com there can be .in 

var arr = str.split("|");

function domainName(str) {
  var match = str.match(/^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d\.-]+)\.(?:[a-z\.]{2,10})(?:[\w\.-]*)*/);
  if (match != null && match.length > 0) {
    return match;
  } else {
    return null;
var domainname = domainName(str);
var domain = domainname;

As CertainPerformance Suggest you can exclude the url that is in [ ] using replace then by using regex you can extract the domain name. Below is the code :

var str = '[[www.abc.com/corporate/partner/just-a-test]]acdascvdvsa.1563e24e32e42|[[www.abc.com/corporate/partner/just-a-test]]1563e24e32e42.1563e24e32e42|[[www.abc.com/instruments/infrared-guided-measurement/]]www.google.com&1566805689640.1566806059701.3';

var str = str.replace(/\[\[[^[\]]*\]\]/g, '');

var ptrn = /^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d\.-]+)\.(?:[a-z\.]{2,10})(?:[\w\.-]*)*/g;

var i, value, domain, len, array;

array = str.split("|");

len = array.length;

for(i=0; len > i; i++) {

  value = array[i].match(ptrn);

  if (value !== null) {
    domain = value;
  else {
     domain = "Not Found";


document.write("Domain is = ", domain);


How To Get The Domain From A URL Using JavaScript, var url = "http://scratch99.com/web-development/javascript/" ; var domain = url. replace( 'http://' , '' ). replace( 'https://' , '' ). 12 responses on “ How To Get The Domain From A URL Using JavaScript ” Edward Caissie August 29, 2011 at 10:46 pm. Just a quick note, you might want to take into consideration ‘https’, for example, https://twitter.com returns https when you use the “Extract” above. Otherwise, I like it.

Replace all occurrences of [[, followed by non-brackets, followed by ]] with a space::

var str = '[[www.abc.com/corporate/partner/just-a-test]]acdascvdvsa.1563e24e32e42|[[www.abc.com/corporate/partner/just-a-test]]1563e24e32e42.1563e24e32e42|[[www.abc.com/instruments/infrared-guided-measurement/]]www.google.com&1566805689640.1566806059701.3';

const result = str.replace(/\[\[[^[\]]*\]\]/g, ' ');

Parsing Hostname and Domain from a Url with Javascript, Parsing urls to extract the hostname and domain can present its own set of The Javascript for parsing the hostname from a url appears as follows: if (match != null && match.length > 2 && typeof match[2] === 'string'� The position where to start the extraction. First character is at index 0. If start is positive and greater than, or equal, to the length of the string, substr () returns an empty string. If start is negative, substr () uses it as a character index from the end of the string. If start is negative or larger than the length of the string, start is set to 0.

Daily Challenge #89 - Extract domain name from URL - DEV, Note: Assuming the string will always start with either http:// or https:// or nothing but not supporting any other protocols (by lazyness). Note2: This� We can extract the domain from a url by leveraging our method for parsing the hostname. Since the above getHostName () method gets us very close to a solution, we just need to remove the sub-domain and clean-up special cases (such as.co.uk). Our Javascript code for parsing the domain from a url appears as follows: 1

this can be achieve by split

var str = '[[www.abc.com/corporate/partner/just-a-test]]acdascvdvsa.1563e24e32e42|[[www.abc.com/corporate/partner/just-a-test]]1563e24e32e42.1563e24e32e42|[[www.abc.com/instruments/infrared-guided-measurement/]]www.google.com&1566805689640.1566806059701.3';
let ans=str.split("]")[6]
let finalAns=ans.split("&")[0]

JS: Extract domain name from URL, This method will find and extract the domain name (e.g. 'codepen.io') from a URL, stripping out http, www, slashes, port number, url params and such. Definition and Usage. The substring () method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between "start" and "end", not including "end" itself. If "start" is greater than "end", this method will swap the two arguments, meaning str.substring (1, 4) == str.substring (4, 1).

var dirtySource = "https://example.com/subdirectory";
var dirtyPos = dirtySource.indexOf("://"); // works for http and https
var cleanSource = dirtySource.substr(dirtyPos+3);
cleanSource = cleanSource.split("/")[0]; // trim off subdirectories

Get URL and URL Parts in JavaScript, JavaScript can access the current URL in parts. For this So to get the full URL path in JavaScript: you can split the string on “/” characters I want to get the domain part from an email address, in Javascript. It's easy to extract the domain from an email like via split: "joe@example.com", which is example.com. However, emails also come in forms like "joe@subdomain1.example.com.uk", of which the domain is example.com.uk, instead of subdomain1.example.com.uk.

extract-domain, protocol property is used to return the protocol scheme of the URL along with the final colon(:). The location.hostname is used to return the domain name of the� The JavaScript string match method returns an array, and because I want this part that I’ve captured, I return the 1st element of the array. (With the match method, the 0th array element is the entire input string, which can be both helpful, bizarre, and confusing.) A little regex explanation. Okay, here’s a short explanation of that regex:

How to get protocol, domain and port from URL using JavaScript , How to write a regular expression to use to extract the domain name from the These expressions can be used for matching a string of text, find and replace getting the domain or host name from a URL using JavaScript. net daarack02. I need to extract the username from domain/username combo derived using XPath Navigator (GetXPathNav). Unfortunately, I can't use basic string manipulation because the slash "/" is being translated as an escape sequence.

Regex extract domain from email, The Excel LEN function returns the length of a given text string as the number of characters. LEN will also count characters in numbers, but number formatting is� The best solution I have came up with to work on multiple projects is using four methods inside an object. First method: is to actually get a substring from between two strings (however it will find only one result).

  • dude did you want only "www.google.com&1566805689640.1566806059701.3"
  • Only www.google.com regardless of its position and the domain can be start with htttps and can have .in instead of .com at the end
  • Thanks for your answer CertainPerformance! Your Answer is some what useful :)
  • Thanks for your answer AndroidNoobie! But its not the desire output, i have edited the question please check that.
  • The process would be the same, you'd just use a different regex to match the desired pattern.
  • i know i need different regex see i also using regex but that not proving desire result.
  • Thanks Once again AndroidNoobie for your valuable time and answer. your answer is useful. But i just want to extract url till & so the correct output will be www.google.com
  • Thanks for your answer akshay! But its not the desire output, i have edited the question please check that.
  • its the right answer, if i have string which include the url every time at the same place , but that not going to happened url can be at different position and i just want to write code only once to get the url regardless of its position, so i [6] will not give the correct output if string going to change. code must be dynamic
  • Please try explaining what you are proposing as the answer. It will help the users.
  • FWIW I did a Google search for "extract domain from a string" and this post came up. Nobody had a simple answer here so I had to code this myself.
  • If the title of the question is incorrect, then I'll remove my answer.
  • The title is misleading, I agree. The actual problem is not solved by your answer. And this post of yours came into the Low Quality Posts queue. Please read this meta.stackoverflow.com/questions/345719/…