const regex = /^(?<domain>[^/?#]+) wants you to sign in with your Ethereum account:\n(?<address>0x[a-zA-Z0-9]{40})\n\n((?<statement>[^\n]+)\n)?\nURI: (?<uri>(([^:?#]+):)?(([^?#]*))?([^?#]*)(\?([^#]*))?(#(.*))??)\nVersion: (?<version>1)\nChain ID: (?<chain_id>[0-9]+)\nNonce: (?<nonce>[a-zA-Z0-9]{8,})\nIssued At: (?<issued_at>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9])))(\nExpiration Time: (?<expiration_time>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\nNot Before: (?<not_before>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\nRequest ID: (?<request_id>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?(\nResources:(?<resources>(\n- (([^:?#]+):)?(([^?#]*))?([^?#]*)(\?([^#]*))?(#(.*))??)+))?$/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<domain>[^\/?#]+) wants you to sign in with your Ethereum account:\\n(?<address>0x[a-zA-Z0-9]{40})\\n\\n((?<statement>[^\\n]+)\\n)?\\nURI: (?<uri>(([^:?#]+):)?(([^?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))??)\\nVersion: (?<version>1)\\nChain ID: (?<chain_id>[0-9]+)\\nNonce: (?<nonce>[a-zA-Z0-9]{8,})\\nIssued At: (?<issued_at>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\\.[0-9]+)?(([Zz])|([+|\\-]([01][0-9]|2[0-3]):[0-5][0-9])))(\\nExpiration Time: (?<expiration_time>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\\.[0-9]+)?(([Zz])|([+|\\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\\nNot Before: (?<not_before>([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\\.[0-9]+)?(([Zz])|([+|\\-]([01][0-9]|2[0-3]):[0-5][0-9]))))?(\\nRequest ID: (?<request_id>[-._~!$&\'()*+,;=:@%a-zA-Z0-9]*))?(\\nResources:(?<resources>(\\n- (([^:?#]+):)?(([^?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))??)+))?$', 'g')
const str = `service.org wants you to sign in with your Ethereum account:
0xe5A12547fe4E872D192E3eCecb76F2Ce1aeA4946
I accept the ServiceOrg Terms of Service: https://service.org/tos
URI: https://service.org/login
Version: 1
Chain ID: 1
Nonce: 12341234
Issued At: 2022-03-17T12:45:13.610Z
Expiration Time: 2023-03-17T12:45:13.610Z
Not Before: 2022-03-17T12:45:13.610Z
Request ID: some_id
Resources:
- https://service.org/login`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
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