3

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?

asked Aug 1, 2015 at 8:48
3
  • what about not http(s):// urls ? ftp:// etc ? Commented Aug 1, 2015 at 8:56
  • can URL contains all type of characters along with special characters? Commented Aug 1, 2015 at 13:56
  • See here stackoverflow.com/q/6038061/1066234 Commented Nov 27, 2023 at 18:27

3 Answers 3

15

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

answered Aug 1, 2015 at 8:57
4
  • no that only returns the part of the url after the /, and only the first segment. I want the whole URL Commented Aug 1, 2015 at 8:58
  • That's because you made wrong. Make group like this... /^(http[s]?:\/\/.*?\/[a-zA-Z-_]+.*)$/ Commented Aug 1, 2015 at 9:01
  • Updated my answer. Try it. Commented Aug 1, 2015 at 9:22
  • This also matches the next line when the URL is at the end of the previous line. Commented Nov 27, 2023 at 18:26
3
  • 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.

Regex explanation

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

answered Aug 1, 2015 at 9:26
-1

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>

mplungjan
180k29 gold badges183 silver badges246 bronze badges
answered Aug 1, 2015 at 8:55
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.