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
  • 3
  • 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
  • 19,392
  • 11
  • 68
  • 116
  • Thanks for your code but when i check with /inc/Docs/Some.pdf it returns the same! it should return inc/Docs/Some.pdf! can u alter ur code – Mazher Jul 22 '13 at 09:35
  • and one more thing when i put something https://something.domain.com/inc/Docs/soem.pdf it doesnt work – Mazher Jul 22 '13 at 09:37
  • @Mazher about something.domain.com/inc/Docs/soem.pdf it will be always dots in server name? Or something.domain.com is static? – Piotr Stapp Jul 22 '13 at 09:43
  • @Mazher mark it as answer or vote it up instead of writing: thank you – Piotr Stapp Jul 22 '13 at 10:49
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
  • 146,340
  • 25
  • 209
  • 204
0

Given a variable link,

var link = link.replace("https://localhost/", "")
Ian Clark
  • 9,237
  • 4
  • 32
  • 49
0

Try this code :

var newurl = url.replace("https://localhost/", "")
Lucas Willems
  • 6,673
  • 4
  • 28
  • 45
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,486
  • 10
  • 51
  • 86