I want to extract the first valid URL in a string, which can be anywhere between characters and whitespace
I have tried with the following
...
urlRegex: /^(http[s]?:\/\/.*?\/[a-zA-Z-_]+.*)$/,
...
var input = event.target.value // <--- some string;
var url = input.match(this.urlRegex);
Problem is url
returns the whole string when it finds a url, instead of returning just the part of the string matching the regex
Example The string
https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd
returns
["https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd", "https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd", index: 0, input: "https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd"]
How can this be achieved?
-
what about not http(s):// urls ? ftp:// etc ?zb'– zb'2015年08月01日 08:56:37 +00:00Commented Aug 1, 2015 at 8:56
-
can URL contains all type of characters along with special characters?Braj– Braj2015年08月01日 13:56:04 +00:00Commented Aug 1, 2015 at 13:56
-
See here stackoverflow.com/q/6038061/1066234Avatar– Avatar2023年11月27日 18:27:27 +00:00Commented Nov 27, 2023 at 18:27
3 Answers 3
Your regex
is incorrect.
Correct regex for extracting URl : /(https?:\/\/[^ ]*)/
Check out this fiddle.
Here is the snippet.
var urlRegex = /(https?:\/\/[^ ]*)/;
var input = "https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd";
var url = input.match(urlRegex)[1];
alert(url);
-
no that only returns the part of the url after the
/
, and only the first segment. I want the whole URLTarlen– Tarlen2015年08月01日 08:58:37 +00:00Commented Aug 1, 2015 at 8:58 -
That's because you made wrong. Make group like this...
/^(http[s]?:\/\/.*?\/[a-zA-Z-_]+.*)$/
Shrinivas Shukla– Shrinivas Shukla2015年08月01日 09:01:45 +00:00Commented Aug 1, 2015 at 9:01 -
Updated my answer. Try it.Shrinivas Shukla– Shrinivas Shukla2015年08月01日 09:22:42 +00:00Commented Aug 1, 2015 at 9:22
-
This also matches the next line when the URL is at the end of the previous line.Avatar– Avatar2023年11月27日 18:26:59 +00:00Commented Nov 27, 2023 at 18:26
- You haven't included digits in your regex as part of URL.
- Assuming URL starts from the beginning of the string
Live Demo with regex explanation on left side.
var regex = /^(https?:\/\/[^/]+(\/[\w-]+)+)/;
var str = 'https://medium.com/aspen-ideas/there-s-no-blueprint-26f6a2fbb99c random stuff sd';
var url = str.match(regex)[0];
document.write(url);
That's because the match result holds the whole string first that matches, then the groups. I guess you want the group, so you can do this:
url[1]
Here's a fiddle: http://jsfiddle.net/jgt8u6pc/1/
var urlRegex = /^http[s]?:\/\/.*?\/([a-zA-Z-_]+).*$/;
var input = 'http://stackoverflow.com/questions/31760030/extracting-for-url-from-string-using-regex' // <--- some string;
var url = input.match(urlRegex);
$('#one').text(url[0]);
$('#two').text(url[1]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="one"></div>
<div id="two"></div>