Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Why this regex doesn't match last alphanumeric character?

I'm trying to figure out why some regular expression doesn't match last alphanumeric character of some given text that may or may not contain a YouTube URL.

The whole regular expression is /(?:https|http):\/\/(?:www\.){0,1}(?:youtube\.com|youtu\.be)\/(?:watch\?v=){0,1}(\w+)[^\s]+/mig (it matches a youtube.com or youtu.be URL until it finds a white space).

Here's a code snippet which works as example about my issue (it shows first regular expression's match as JSON):

var match = /(?:https|http):\/\/(?:www\.){0,1}(?:youtube\.com|youtu\.be)\/(?:watch\?v=){0,1}(\w+)[^\s]+/mig.exec("https://www.youtube.com/watch?v=8C6xDjQ66wM");

document.getElementById("result").textContent = JSON.stringify(match);
<div id="result"></div>

If you execute the so-called regular expression, it'll match YouTube video id, but it lacks the last character (it should match 8C6xDjQ66wM but it matches 8C6xDjQ66w).

Upvotes: 0

Views: 111

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

If you insist on your own regex, here is a fixed version that includes the last letter of the URL into the capture group (just moved [^\s]+ to (\w+[^\s]+)):

var match = /(?:https|http):\/\/(?:www\.){0,1}(?:youtube\.com|youtu\.be)\/(?:watch\?v=){0,1}(\w+[^\s]+)/mig.exec("https://www.youtube.com/watch?v=8C6xDjQ66wM");
document.getElementById("result").textContent = JSON.stringify(match);

    
<div id="result"></div>

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

The [^\s]+ requires at least one non-space character to satisfy the expression, so it takes the M. Seems like you could do [^\s]* or perhaps (?:[^\s]+|$) -- anything that doesn't require at least one additional character.

Upvotes: 1

Related Questions