1

Having a string of the below structure how can i extract only the link portion of it. https://qr.1688.com/s/OaPpTrzR CZ0736

https://qr.1688.com/s/2Q8NXya1 CZ3050

const getShortUrl = (string)  =>{
    let query = string.trim().replaceAll('"', '');
    if(query.indexOf('https://qr') >= 0){
          return query.substring(query.indexOf('https://qr'));
        
    }
  }


But the above code return something like `https://qr.1688.com/s/2Q8NXya1 CZ3050` but what i wanted is something like this `https://qr.1688.com/s/2Q8NXya1`

how can i extrract this portion of string.
2

2 Answers 2

3

You can use split:

return query.split(' ')?.[0];
Sign up to request clarification or add additional context in comments.

Comments

3

You can split the string into parts by the space with the .split(" ") method. And then just use the first element of the created array.

Example

query = query.split(" ")[0]

With your code

const getShortUrl = (string) => {
  let query = string.trim().replaceAll('"', '');
  query = query.split(" ")[0]
  if (query.indexOf('https://qr') >= 0) {
    return query.substring(query.indexOf('https://qr'));

  }
}

const shortUrl = getShortUrl("https://qr.1688.com/s/OaPpTrzR CZ0736")
console.log(shortUrl)

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.