0

I have a string like:

link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755|
link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf|
link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|

I need to extract the second URL:

https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf

Each URL is contained between 2 sticks and changes with each script execution, but the extension is always the same (* .pdf).

1 Answer 1

2

I believe your goal as follows.

  • You want to retrieve https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf from the following string using Google Apps Script.

    const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755|
    link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf|
    link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`;
    

For this, how about this answer?

Pattern 1:

In this pattern, split is used. In this case, when the position of the URL you want is the same, this can be used.

Sample script:

const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755|
link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf|
link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`;

const res = str.split("|")[3];
console.log(res)

Pattern 2:

In this pattern, regex is used. In this case, when the URL you want is enclosed by link_of_pdf| and |, this can be used.

Sample script:

const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755|
link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf|
link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`;

const res = str.match(/link_of_pdf\|(.+)\|/)[1];
console.log(res)

References:

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

2 Comments

Perfect, I had been working with a code similar to your first solution, but the second one assures me the same result even if I changed the position of the URL. Thank you.
@J. Kenneth Reátegui B. Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.