0

I have a few comma-separated URLs as shown below.

https://drive.google.com/open?id=17kTY2b3XvERC4wqZnLt7sVwMe8ZoDZUD, https://drive.google.com/open?id=1En1tNNLEgz5JiIk2GJnjNb_bhk23YJb2, https://drive.google.com/open?id=2En1tNNLEgz5JiIk3GJnjNb_bhk23YJb2

I need to get just the IDs (Example: 17kTY2b3XvERC4wqZnLt7sVwMe8ZoDZUD) from it in an array.

I'm a rookie coder and would appreciate it if someone can help me with the code.

2 Answers 2

5

Use the URL parsing library built-in to the browser. Don't attempt to use regular expressions to pick apart the URL because you don't have to (and you might end up getting it wrong).

var u = new URL("https://drive.google.com/open?id=2En1tNNLEgz5JiIk3GJnjNb_bhk23YJb2");
console.log(u.searchParams.get('id'));
> "2En1tNNLEgz5JiIk3GJnjNb_bhk23YJb2"
Sign up to request clarification or add additional context in comments.

Comments

0

You could use string.split("?id=") and get the second element from the array returned (element 0 for everything to the left, 1 for the right)

E.G:

<script>
    var myArray = ["https://drive.google.com/open?id=17kTY2b3XvERC4wqZnLt7sVwMe8ZoDZUD", 
    "https://drive.google.com/open?id=1En1tNNLEgz5JiIk2GJnjNb_bhk23YJb2", 
    "https://drive.google.com/open?id=2En1tNNLEgz5JiIk3GJnjNb_bhk23YJb2"];

    for(var i=0; i<myArray.length; i++)
    {
        var result = myArray[i].split("?id=")[1];
        console.log(result);
    }
</script>

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.