0

I would like to achieve the following:

I have a URL: https://www.example.com/Place/Name.html?randomtext

I need to return "Name" only on outgoing links.

I thought it would work by doing the following:

    function() {
      if ({{outgoing link}})
      var Name= {{Click URL}};
      return Name.split("/")[2];
      return Name.split("?")[0];
    }

I already managed to get "place" by doing:

    function() {
          if ({{outgoing link}})
          var Name= {{Click URL}};
          return Name.split("/")[2];
2
  • I am not 100% I get what you are trying to do here? Commented May 19, 2022 at 14:43
  • Does this answer your question? How to split url to get url path in JavaScript Commented May 19, 2022 at 14:43

2 Answers 2

0

One way to get the filename ("Name") would be to do:

var filename = url.substring(url.lastIndexOf('/')+1).split(".")[0]

https://codesandbox.io/s/magical-roman-uerpcp?file=/src/index.js

Sign up to request clarification or add additional context in comments.

Comments

0

not sure if this is what u want:

const url = 'https://www.example.com/Place/Name.html?randomtext'

 function extractSliceFromUrl() {
      return url.split("/")[4].split(".")[0];
    }
    

const url = 'https://www.example.com/Place/Name.html?randomtext'

 function extractSliceFromUrl() {
      return url.split("/")[4].split(".")[0];
    }
    
    console.log(extractSliceFromUrl(url))

    console.log(extractSliceFromUrl(url))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.