I want the output as Test_guest1613 if the pattern is for Guest Name and AK004 if the pattern is for Room Number
let mystring = `The description value is Holrest
Code: adip-345
Guest Name : Test_guest1613
Room Number: AK004
Request Number : 107157
Request Method: Email
Dates Requested:25-Jul-2020 08:00PM
If no specific date, weekday or weekend : N/A
Start Time:25-Jul-2020 08:00PM
Time Zone : ET `;
re=/Guest Name\s?:\s?(.*?)( |$)/g;
mystring.match(re);
gives me the array output Guest Name: Test_guest1613 but i want just want Test_guest1613
Use re.exec(mystring)
to collect captured groups and access the second in the array.
let mystring = `The description value is Holrest Code: adip-345 Guest Name: Test_guest1613 Room Number: AK004 Request Number: 107157 Request Method: Email Dates Requested:25-Jul-2020 08:00PM If no specific date, weekday or weekend: N/A Start Time:25-Jul-2020 08:00PM Time Zone: ET `; re=/Code\s?:\s?(.*?)(?:\n| |$)+Guest Name\s?:\s?(.*?)(?: |$)/gm; let matches = re.exec(mystring); let code = matches[1]; let guestName = matches[2]; console.log(guestName, code);
You could remove Guest Name:
after the fact with .replace
. IE: mystring.match(re).replace(/Guest Name\s?:\s?/)
If you want both values for Guest Name
and Room Number
, you could use 2 capturing groups:
^Guest Name\s?:\s?(.*?)\s*\r?\nRoom Number\s?:\s?(.*)
These are the separate pattern, for which you could use group 1 to get the value
^Guest Name\s?:\s?(.*?)[^\S\r\n]*$
and ^Room Number\s?:\s?(.*?)[^\S\r\n]*$
const regex = /^Guest Name\s?:\s?(.*?)\s*\r?\nRoom Number\s?:\s?(.*)/m; const str = `The description value is Holrest Code: adip-345 Guest Name: Test_guest1613 Room Number: AK004 Request Number: 107157 Request Method: Email Dates Requested:25-Jul-2020 08:00PM If no specific date, weekday or weekend: N/A Start Time:25-Jul-2020 08:00PM Time Zone: ET `; let m = str.match(regex); console.log(m[1]); console.log(m[2]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.