1

i am manually entering a url link in a Html form which might be

https://localhost/inc/Pega/Some.pdf or inc/Pega/Some.pdf ,

i need to check whether the url contains any link i.e https

1) if it contains then i have to strip text link to

'inc/Pega/Some.pdf' 
Mazher
  • 91
  • 1
  • 13

6 Answers6

1

You can use following JavaScript:

var url = "https://localhost/inc/Pega/Some.pdf";
url = url.replace(/^(http[s]*:\/\/[a-zA-Z0-9_]+)*\//,"")

Now explanation: From the begging of string (^) I remove protocol (http or https) then everything between :// and /, which is letters, numbers or underscore. If link will not start with http:// or https:// or / nothing will be changed

Piotr Stapp
  • 18,130
  • 10
  • 63
  • 104
1

You can the required part of url using substring

Live Demo

if(url.indexOf('https:') == 0)
   $('#text1').val(url.substring(url.indexOf('inc/Pega')));
Adil
  • 139,325
  • 23
  • 196
  • 197
0

Given a variable link,

var link = link.replace("https://localhost/", "")
Ian Clark
  • 8,852
  • 4
  • 29
  • 47
0

Try this code :

var newurl = url.replace("https://localhost/", "")
Lucas Willems
  • 5,931
  • 2
  • 25
  • 41
0

Run this JS function while submitting the form :

function check_URL_Is_Valid(url){
  var regular_exp = new RegExp("^(http|https)://", "i");
  var given_url = url;
  var match = regular_exp.test(given_url);
  if (match){
    alert('URL is valid');
    var sub_url = given_url.match(/^http[s]?:\/\/.*?\/([a-zA-Z-_]+).*$/)[0];
    alert('SubURL='+sub_url);
  }else{
    alert('URL is Invalid');
  }
}

I hope this will fulfill your requirement. Please let me know if you face any problem.

Rubyist
  • 6,247
  • 8
  • 46
  • 83