Home > Blockchain >  How to get each youtube url from textarea in JavaScript?
How to get each youtube url from textarea in JavaScript?

Time:01-03

Please solve my problem. I have a textarea. In this textarea i enter multiple youtube video url(line by line). My question is how to store each url as array using javascript. Thanks in advance.

CodePudding user response:

This code should works, I split the textarea value based on line break as you mentioned (line by line).

 let textarea = document.querySelector('textarea')
    let arr = []
    function check(){
      arr = textarea.value.split('\n')
      console.log(arr)
    }
<textarea>https://www.youtube.com/watch?v=EqboAI-Vk-U&t=22s
https://www.youtube.com/channel/UCK8sQmJBp8GCxrOtXWBpyEA</textarea>
 <button onclick = 'check()'>Check</button>

CodePudding user response:

The simplest way would be to use regex like this (textareaValue contains the value of textarea):

const matches = textareaValue.match(/\bhttp(s)?:\/\/(www.)?youtu(be|.be).*\b/gm);

The matches will be an array of the urls that start with http(s)://(www.)youtube

  • Related