const regex = /(?<=Project\s+#.*\r?\n?)(?<Item>.+)\s+\r?\n?(?:Total\s+Cost:\s+)(?<TotalCost>[\d.,\$]+)\r?\n?(?:City\s+Obligation:\s+)(?<CityObligation>[\d.,\$]+)\r?\n?(?:Delivery\s+Method:\s+)(?<DeliveryMethod>.+)\r?\n?(?:Cooperating\s+Agencies:\s+)(?<CooperatingAgencies>.+)\r?\n?\r?\n?(?:Special\s+Circumstances)\r?\n?\r?\n?(?<SC>.+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=Project\\s+#.*\\r?\\n?)(?<Item>.+)\\s+\\r?\\n?(?:Total\\s+Cost:\\s+)(?<TotalCost>[\\d.,\\$]+)\\r?\\n?(?:City\\s+Obligation:\\s+)(?<CityObligation>[\\d.,\\$]+)\\r?\\n?(?:Delivery\\s+Method:\\s+)(?<DeliveryMethod>.+)\\r?\\n?(?:Cooperating\\s+Agencies:\\s+)(?<CooperatingAgencies>.+)\\r?\\n?\\r?\\n?(?:Special\\s+Circumstances)\\r?\\n?\\r?\\n?(?<SC>.+)', 'gm')
const str = `Facilities Management
Project #: 0450-CARP
Replace Carpet - City Hall
Total Cost: \$243,203
City Obligation: \$200,000
Delivery Method: TBD
Cooperating Agencies: None
Special Circumstances
None
Project Description Location: One City Plaza - City Hall
Remove and Replace the carpet at City Hall.
Project Justification
The carpet at City Hall is 18 years old, with some areas failing and causing a trip hazard.
The carpet needs replacement in phases over the next 4-5 years, beginning in Fiscal
Year 20 and ending in Fiscal Year 24.
Budget Impact/Other
Statement of Impact: There is no impact to the City’s operating budget.
Expenditures Previous 2021 2022 2023 2024 2025 Out Total
Years Years
Other \$43,203 \$50,000 \$50,000 \$50,000 \$50,000 \$243,203
Total \$43,203 \$50,000 \$50,000 \$50,000 \$50,000 \$243,203
Funding Previous 2021 2022 2023 2024 2025 Out Total
Sources Years Years
General Fund \$43,203 \$50,000 \$50,000 \$50,000 \$50,000 \$243,203
Total \$43,203 \$50,000 \$50,000 \$50,000 \$50,000 \$243,203
FY 2021 - FY 2025 Capital Improvement Program Page 18 Effective July 1, 2020`;
// 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