Home > Back-end >  Extract URL until whitespace or <br> tag using Regex with Javascript
Extract URL until whitespace or <br> tag using Regex with Javascript

Time:12-25

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;

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

CodePudding user response:

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

  • Related