const regex = new RegExp('HTTP\\/\\d\\.\\d\\s(\\d{3})', 'g')
const str = `HTTP/1.1 302 See other
Date: Tue, 17 Feb 2015 20:20:48 GMT
Server: Apache
X-Powered-By: PHP/5.5.21
Set-Cookie: 5e8627afafd16a920dac4dbb644715a5=ue1gprimq6rb5r4qsb8sk28jb2; path=/; HttpOnly
Location: http://fake.dk/da/
Connection: close
Content-Type: text/html; charset=utf-8
Set-Cookie: SERVERID=; path=/
HTTP/1.1 200 OK
Date: Tue, 17 Feb 2015 20:20:48 GMT
Server: Apache
X-Powered-By: PHP/5.5.21
Set-Cookie: 76726d50abd3edd601ecfbc19fe61c87=da-DK; path=/
Set-Cookie: 76726d50abd3edd601ecfbc19fe61c87=da-DK
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Expires: Mon, 1 Jan 2001 00:00:00 GMT
Last-Modified: Tue, 17 Feb 2015 20:20:48 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Connection: close
Content-Type: text/html; charset=utf-8
Set-Cookie: SERVERID=; path=/
<!doctype html>
<html lang="da">
<head>
</head>
<body>
</body>
</html>`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions