#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?ims)(?<=(?:\[embed\])|(?:\[embed\]\s)|(?:))(https?:\/\/)(:?www\.)?(:?youtube\.com\/watch|youtu\.be\/)([\w\?=\&]+)(?=(?:\[\/embed\])|(?:\s\[\/embed\])|(?:))"
Local $sString = "bla https://www.youtube.com/watch?v=Vpg9yizPP_g" & @CRLF & _
"http://www.youtube.com/watch?v=Vpg9yizPP_g bla" & @CRLF & _
"[embed]https://www.youtube.com/watch?v=Vpg9yizPP_g[/embed] bla" & @CRLF & _
"[embed] https://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]" & @CRLF & _
"[embed]http://www.youtube.com/watch?v=Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] http://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]" & @CRLF & _
"bla https://youtube.com/watch?v=Vpg9yizPP_g bla" & @CRLF & _
"http://youtube.com/watch?v=Vpg9yizPP_g" & @CRLF & _
"[embed]https://youtube.com/watch?v=Vpg9yizPP_g[/embed]bla" & @CRLF & _
"bla[embed] https://youtube.com/watch?v=Vpg9yizPP_g [/embed]" & @CRLF & _
"[embed]http://youtube.com/watch?v=Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] http://youtube.com/watch?v=Vpg9yizPP_g [/embed]" & @CRLF & _
"https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g" & @CRLF & _
"http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g" & @CRLF & _
"[embed]https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]" & @CRLF & _
"[embed]http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]" & @CRLF & _
"bla[embed] http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]bla" & @CRLF & _
"https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g bla" & @CRLF & _
"bla http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g" & @CRLF & _
"[embed]https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]" & @CRLF & _
"[embed]http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]" & @CRLF & _
"https://youtu.be/Vpg9yizPP_g" & @CRLF & _
"http://youtu.be/Vpg9yizPP_g bla" & @CRLF & _
"[embed]https://youtu.be/Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] https://youtu.be/Vpg9yizPP_g [/embed]" & @CRLF & _
"[embed]http://youtu.be/Vpg9yizPP_g[/embed]" & @CRLF & _
"[embed] http://youtu.be/Vpg9yizPP_g [/embed]" & @CRLF & _
"<a href src="http://youtu.be/Vpg9yizPP_g">vid</a>" & @CRLF & _
"<a href src="http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g">vid</a>" & @CRLF & _
"<a href src="https://youtube.com/watch?v=Vpg9yizPP_g">vid</a>"
Local $sSubst = "\1\2\3\4"
Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst)
MsgBox($MB_SYSTEMMODAL, "Result", $sResult)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm