0

Apologies in advance as I know there are so many answers to this question however I have not been successful with implementing any of the given answers. With that being said, let's get to the issue.

I have a table containing text inputs where user enters youtube video ids
Onsubmit my javascript will create an array containing these youtube video ids
If user enters a youtube url, the array breaks

Quick Solution
If someone can help me get the regex validation to work so the user will be alerted properly and correct their input that would be a good start

Ideal Solution
I'm not fond of alerting users and making them change what they've entered as its bad user experience so if anyone is feeling extra helpful today and could show me how to take what they enter and strip out the url bits and push only the youtube video id to the array that would be super wonderful :)

example: if user entered

["https://www.youtube.com/watch?v=K1CScQOj1dA", "https://youtu.be/wIJ1sFrgMds", "IISzG-sPUuo"]

the code would format their inputs and the final userSongs array would be:

["K1CScQOj1dA", "wIJ1sFrgMds", "IISzG-sPUuo"]



Here is my fiddle http://jsfiddle.net/1qLr9n2j/
As you can see, the alert says no match when two of the inputs are invalid entries

5
  • Why (new RegExp('\\b' + userSongs.join('\\b|\\b') + '\\b'))? Commented Jun 2, 2015 at 19:34
  • I found that solution here -> stackoverflow.com/questions/6219960/… Commented Jun 2, 2015 at 19:40
  • You just want to transform userSongs array? Commented Jun 2, 2015 at 19:42
  • Yes I'd preferably like to transform the array and not have to alert the user. Whichever solution is easiest. Commented Jun 2, 2015 at 19:43
  • Check out the answers here and customize this for your use? stackoverflow.com/questions/5830387/… Commented Jun 3, 2015 at 5:08

2 Answers 2

1

You can use the following to match:

.*?([\w-]+)$

And replace with $1.

See DEMO

JS Code:

var rx = /.*?([\w-]+)$/;
for (var i = 0; i < userSongs.length; i++) {
        userSongs[i] = userSongs[i].replace(rx, '$1');
}
alert(userSongs);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but how would I put this into my if statement?
Wow! How did you reduce the regex from /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/ only down to /.*?([\w-]+)$/ ? :)
check your code.. thats because you are dealing only with ids and not the actual url pattern..
1

Here's my crack at the Ideal Solution

var re = new RegExp("youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)");

$('.song-input').each(function () {
    var val = $(this).val()
    if (val) {
        var matches = val.match(re);
        $(this).val(matches[1]);
    }
});

I took the regex from James Poulson's answer to the question you linked to.

3 Comments

Looks awesome, but I'm getting an Unexpected token } error when I put this in my fiddle. Advice?
Hmm, I actually copied and pasted this from a Fiddle myself; must be unclear exactly which part this is intended to replace? Try this: jsfiddle.net/1qLr9n2j/2
Ok thanks. I see it changing the input values. So in your fiddle should I just add userSongs.push($(this).val()); to line 19 before the } get my formatted array now?

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.