Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys,

I came around a situation where i have to extract a particular string on a given URLs/Sites :-

suppose URL is :-

www.yahoo.com/sites/spec/aaa/aaa.aspx
https://facebook.com/sites/test/bbb/test.aspx
uat-abc-link/project/sites/fastplay/kwott/site-page/dbs.html
https://amst-tpage/speg/minaral-water/pages/mpower.aspx
www.marklet/mplay/extra.fff/chart.aspx


Now my concern is - I want to get URL till spec/test/speg/fastplay/mplay and remove everything after that words.

So my output would be :-

www.yahoo.com/sites/spec
https://facebook.com/sites/test
uat-abc-link/project/sites/fastplay
https://amst-tpage/speg
www.marklet/mplay

Please note that - Not every URLs/ Sites have .com domain.

Thanks in advance....

What I have tried:

I tried to do substring. But there is no particular identifier like Sites/speg/fastplay/mplay. So not able to acheive task.
Posted
Updated 19-Dec-17 21:41pm
Comments
Afzaal Ahmad Zeeshan 20-Dec-17 2:34am    
There is no particular logic working behind this approach. I cannot see any. Somewhere you are trimming last two elements, somewhere, three. Sometimes your string is having an element with "-" to be removed, somewhere it is just the last two.

Who asked you to solve this?

Looks like a homework, if so, please ask your teacher to make it solveable.
Sinisa Hajnal 20-Dec-17 2:35am    
In essence, you need either first thing after base url or first thing after baseurl/sites. Not that hard to do, try it and let us know.

1 solution

try

function getUrl(url) {
          var final =
          url = url.toLowerCase(); var target = [];
          var temp = url.split('/');
          if (url.indexOf('.com') > -1) {

              var count = temp.indexOf('sites')
                  count = count + 2;
              for (var i = 0; i < count  ; i++) {
                  target.push(temp[i]);
              }
              final = target.join('/');
          }
          else {

              var count = 2;
              if (url.indexOf('//') > -1)
                  count = count + 2;
              for (var i = 0; i < count; i++) {
                  target.push(temp[i]);
              }
              final = target.join('/');
          }
          return final;
      }
      getUrl('www.yahoo.com/sites/spec/aaa/aaa.aspx')  //www.yahoo.com/sites/spec
      getUrl('https://facebook.com/sites/test/bbb/test.aspx')  //https://facebook.com/sites/test
      getUrl('uat-abc-link/project/sites/fastplay/kwott/site-page/dbs.html') //"uat-abc-link/project/sites/fastplay"
      getUrl('https://amst-tpage/speg/minaral-water/pages/mpower.asp') //"https://amst-tpage/speg"
      getUrl('www.marklet/mplay/extra.fff/chart.aspx') //www.marklet/mplay"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900