const regex = /\s*([JSESSIONID]+)\s*=\s*(?:"([^"])"|([^;"]*))\s*/;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\s*([JSESSIONID]+)\\s*=\\s*(?:"([^"])"|([^;"]*))\\s*', '')
const str = `HTTP/1.1 302 Found
Date: Fri, 10 Jun 2016 21:44:38 GMT
Set-Cookie: JSESSIONID=1n9rk5co2o4xkpr327clod1pu;Path=/genesys;Expires=Sat, 11-Jun-2016 21:44:38 GMT;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://192.168.1.200:8010/genesys/admin/login.jsp#/
Content-Length: 0`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// 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