var link = '<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=https://www.google.com/">'; link.match(/^ ">$/ig); 

How do I get https://www.google.com/ using a regular expression?

  • one
    How is https://www.google.com/ related to checking the beginning and end? - Grundy
  • @Grundy, I thought I could check the beginning on the URL = and end on "> And get everything in between - uzi_no_uzi
  • The beginning of the line is the first character (or rather, even that before it), the end of the line is the last. And you want to get part of the line from the middle. - Enikeyschik February

2 answers 2

It can be very simple:

 var link = '<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=https://www.google.com/">'; link.match(/URL=(.+)">$/i); 

Result:

 array( 0 => URL=https://www.google.com/"> 1 => https://www.google.com/ ) 

Here is a good cheat sheet: https://www.exlab.net/files/tools/sheets/regexp/regexp.png

 Начало строки: '^' Конец строки: '$' 

In this case, no check at the beginning of the line is needed.

  • Generally, a question on JavaScript, but not on PHP. Why in the answer preg_match('/URL=(.+)">$/i', $input_line, $output_array); ;? - Wiktor Stribiżew February
  • Thanks, corrected. - Roman Salo

You need a preview here:

 link.match(/(?<=URL=).+(?=")/)[0] 

You can still through split() :

 link.split(/URL=|"/)[4] 

And you can also use replace() :

 link.replace(/.+URL=|">/g,"")