JSFiddle - React, Tailwind, and code Playground
HTML
<!-- https://stackoverflow.com/questions/26671307/ -->
<p>Enter a URL: <input type="text" id="domain_text"/></p>
<p>(or)</p>
<p>Paste some text containing URLs in the box below</p>
<textarea cols="60" rows="15" id="domain_textarea">
What i try to do is when i have:
www.gmail.com,
www.gmail.com/,
http://www.gmail.com,
https://www.gmail.com,
http://gmail.com,
https://gmail.com,
www.gmail.com/example
just get gmail.com, by far from searching into relative questions i have pattern to match these things which is:
var pat = /^(https?:\/\/)?(?:www\.)?([^\/]+)/;
but it also returns true when i put also a word eg. gmail (without .com). How can i improve this to match things that are of the form abcd.efgs.com ? I mean specify that the string should contain characters and at least one dot after the http,https,www. ?
Thanks in advance!
</textarea>
<p><strong>click <a href="#">here for result</a>:</strong></p>
JavaScript
$(document).ready(function(){
$('#domain_text, #domain_textarea').blur(function(){
var str = $.trim($(this).val());
var pat = /(?:\w+\.)+\w+/gm;
if (pat.test(str)){
var match = str.match(pat);
$(this).val(match.join('\n'));
}
else{
$(this).val('Validation failed');
}
});
});