1

I'm trying to get youtube id from urls

https://youtu.be/id
https://www.youtube.com/watch?v=id
https://m.youtube.com/watch?v=id

My code:

<form action="http://www.example.com/">
    <input type="text"><br>
    <input type="submit" value="Submit">
</form>

Can anyone guide me on how to get this result?

http://www.example.com/id

Explanation

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
ericdrey
  • 13
  • 4

2 Answers2

1
document.querySelector('input[type=submit]').onclick = function(e) {
  e.preventDefault();
  var uri = document.querySelector('input[type=text]').value;
  var id = /youtu.+(\/|=)(.+)$/.exec(uri)[2];
  alert('http://example.com/'+id);
}

I don't know what to say more. If you have any questions, please leave a comment.

Joshua K
  • 2,407
  • 1
  • 10
  • 13
0

Check out my simple solution

var url = 'https://www.youtu.be/watch?v=98123712638'; 
var parsedStr = url.split("v=")[1];
parsedStr = (parsedStr != undefined) ? parsedStr : url.split("youtu.be/")[1];
var resultId = parsedStr.split("&")[0];

console.log(resultId);
lankovova
  • 1,396
  • 8
  • 15