skip to Main Content

I hope to get URL from a string, how can I do it with jQuery ?

For example

Source: AAA http://www.google.com/ Target: http://www.google.com/

Source : http://www.msn.com/ This is a comment Target: http://www.msn.com/

Source : BB http://www.twitter.com/ Good site Target: http://www.twitter.com/

Source : Hello https://www.youtube.com Target: https://www.youtube.com

Source : https://stackoverflow.com/74926284 Target: https://stackoverflow.com/74926284

2

Answers


  1. You can use regex

    const string = "BB http://www.twitter.com/ Good site"
    const matches = string.match(/b(http|https)?://S+/gi)[0];
    console.log(matches);
    Login or Signup to reply.
  2. You can take reference from here

    String.prototype.indexOf()

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

    var string = "Hello https://www.youtube.com";
    var url = string.indexOf("https://");
    var resurl = string.substring(url);
    console.log(resurl); 
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search