I have seen some StackOverflow
answers to this question but they all used JavaScript
The question is:- How to get YouTube URLs from a document using Regex in Dart/Flutter
I have a document that comes from my backend and has HTML
tags and it has an embedded YouTube
video in it. But in the end, it's just like a text document, isn't it?
First here is my document that I want to get the YouTube
link from
<figure
class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe
title="| Title here |"
width="1170"
height="878"
src="https://www.youtube.com/embed/OWGnQ61kLzw?feature=oembed" // <-- I want to get this link
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
</div>
</figure>
Here is my Dart
code that I use to get this but I fail
String getYouTubeUrl(String content) {
RegExp regExp = RegExp(
r'^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$');
String matches = regExp.stringMatch(content);
if (matches == null) {
return ''; // Always returns here while the video URL is in the content paramter
}
final String youTubeUrl = matches;
return youTubeUrl;
}
Am I doing something wrong? Is my RegExp
correct? Here is what I want:- https://regexr.com/3dj5t
The RegExp
is actually correct according to the mentioned website but in Dart I can't seem to get it to work
Now, how can I extract the YouTube URL from this document?