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,790
  • 11
  • 66
  • 112
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
  • 143,427
  • 25
  • 201
  • 198
0

Given a variable link,

var link = link.replace("https://localhost/", "")
Ian Clark
  • 9,106
  • 4
  • 31
  • 48
0

Try this code :

var newurl = url.replace("https://localhost/", "")
Lucas Willems
  • 6,311
  • 3
  • 27
  • 43
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,405
  • 9
  • 47
  • 84