Home > Mobile >  Regex substring between two strings
Regex substring between two strings

Time:03-22

How is it possible to get the the full link from

'<a href="https://www.google.com/setprefdomain?prefdom=DE&amp;prev=https://www.google.de/&amp;sig=K_DtcF1dnV7Xn6g9Ir_3SUs6a6TiA=">'

starting at 'href="' and ending at 'A="', but only if this string contains the string "domain"?

I dont really know, how to check, if the string 'domain' is included.

My work so far is: '/(?<=href=")(.*)(?=")/gi'.

CodePudding user response:

From what you have, just add the required string: 'domain' between two "catch-all" rules like this:

Note: removed 'domain' from the second url in my example to show that it only returns the first.

const str = `
<a href="https://www.google.com/setprefdomain?prefdom=DE&amp;prev=https://www.google.de/&amp;sig=K_DtcF1dnV7Xn6g9Ir_3SUs6a6TiA=">

<a href="https://www.google.com/setpref?prefdom=DE&amp;prev=https://www.google.de/&amp;sig=K_DtcF1dnV7Xn6g9Ir_3SUs6a6TiA=">
`;
const regex = /(?<=href=")(.*domain.*)(?=")/gim;
const found = str.match(regex);
console.log(found);

CodePudding user response:

I think @jscrip's answer may be the most straight forward way. Alternatively, you could check to see if the string includes the string 'domain' before matching the regex. For example:

let str = '<a href="https://www.google.com/setprefdomain?prefdom=DE&amp;prev=https://www.google.de/&amp;sig=K_DtcF1dnV7Xn6g9Ir_3SUs6a6TiA=">'

let href = str.includes('domain') ? str.match(/(?<=href=").*(?=")/)[0] : 'Not valid'

console.log(href)

CodePudding user response:

const string =
    '<a href="https://www.google.com/setprefdomain?prefdom=DE&amp;prev=https://www.google.de/&amp;sig=K_DtcF1dnV7Xn6g9Ir_3SUs6a6TiA=">';

let url = string.match(/href="(?<url>.*domain.*)"/i); 

console.log(url.groups.url);

  • Related