Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract URL until whitespace or <br> tag using Regex with Javascript

I have a string like:

Webcam recording https://www.example.com/?id=456&code=123

or like:

Webcam recording https://www.example.com/?id=456&code=123<br><b>test<b>

To extract the URL from the first example I used: var reg_exUrl = /\bhttps?:\/\/[^ ]+/g;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now I tried to extend the Regex so it takes the first match until whitespace (end of line) or <br> tag.

This was my attempt:

var reg_exUrl = /\b(https?:\/\/[^ ]+)(\<br\>)/g;

Which looks good on https://regex101.com/r/gudNab/1 and shows up as two different matches.

But using the Regex in Javascript, the <br> tag gets always included in the link.

Using var matches = line.match(reg_exUrl); gives me with matches[0]:

https://www.example.com/?id=456&code=123<br>

instead of the desired https://www.example.com/?id=456&code=123

>Solution :

If you want to select text before the <br> you can use a postive lookahead.
https?:\/\/.*?(?=<br>)

Adding in a $ and \n for an early end of input: https?:\/\/.*?(?=<br>|$|\n)

const regexp = /https?:\/\/.*?(?=<br>|$|\n)/;
const testString = "Webcam-Aufnahme https://www.edumaps.de/file?id=959559110184937375.mp4&code=4yrn1ev<br>**test**";

console.log(testString.match(regexp)[0])

See on regex101

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading