如何使用正则匹配Salesforce返回HTML字符串中Sentence 1之后的内容
Got it, let's tackle this problem step by step. Here's how you can extract the content after the first sentence following two <br /> tags using regex, along with adaptions for different scenarios:
We'll use a regular expression to target the pattern of two <br /> tags, followed by the first sentence, then capture everything that comes after. Below are implementations tailored to your needs, easily adaptable for Salesforce-related code (like Apex) or frontend scripts.
Basic Implementation (Matching "Sentence 1" specifically)
If you need to target the exact phrase "Sentence 1.", use this regex and code:
// Sample input from your example const htmlString = "Dear {!Contact.FirstName}, <br /> <br /> Sentence 1. Sentence 2. <br /> <br /> Sentence 3. Sentence 4."; // Regex to match two <br /> tags, then "Sentence 1.", then capture all content after const targetRegex = /(?:<br \/>\s*){2}Sentence 1\.(.*)/s; // Execute the match and extract the result const matchResult = htmlString.match(targetRegex); const extractedContent = matchResult ? matchResult[1].trim() : ""; console.log(extractedContent); // Output: "Sentence 2. <br /> <br /> Sentence 3. Sentence 4."
Regex Breakdown
(?:<br \/>\s*){2}: Non-capturing group that matches two instances of<br />, allowing any whitespace (spaces, newlines) between/after the tags.Sentence 1\.: Matches the exact phrase "Sentence 1." (the backslash escapes the period, since periods act as wildcards in regex).(.*): Capturing group that grabs everything after the target phrase. Thesmodifier ensures the period matches newline characters too, so we don't miss content split across lines..trim(): Cleans up any leading whitespace right after the target sentence.
Adaptation for Arbitrary First Sentences
If you need to handle any first sentence (not just "Sentence 1") that follows two <br /> tags, adjust the regex to match any sentence ending with a period:
const flexibleRegex = /(?:<br \/>\s*){2}[^.]+\.(.*)/s; // Example with a different first sentence const anotherHtmlStr = "Hi {!Contact.Name}, <br /><br /> This is my opening line. Here's the rest of my content. <br /> Thanks!"; const flexibleMatch = anotherHtmlStr.match(flexibleRegex); const flexibleResult = flexibleMatch ? flexibleMatch[1].trim() : ""; console.log(flexibleResult); // Output: "Here's the rest of my content. <br /> Thanks!"
Key Adjustment
[^.]+\.: Matches any sequence of characters that doesn't include a period (the first sentence), followed by the ending period. This works for any sentence structure.
Handling Edge Cases
- Self-closing tag variations: If Salesforce returns
<br>instead of<br />, update the regex to support both formats:const compatibleRegex = /(?:<br\/?>\s*){2}[^.]+\.(.*)/s; - Non-period sentence endings: If sentences end with exclamation points or question marks, expand the regex to match those:
const multiPunctRegex = /(?:<br\/?>\s*){2}[^.!?]+[.!?](.*)/s; - Older environments without
smodifier: If your runtime doesn't support thesflag (e.g., older JavaScript versions), replace.*with[\s\S]*(matches all whitespace and non-whitespace characters, including newlines):const legacyRegex = /(?:<br \/>\s*){2}[^.]+\.([\s\S]*)/;
Processing an Array of HTML Strings
To apply this to your array of Salesforce query results, loop through each element and apply the regex:
const htmlArray = [ "Dear {!Contact.FirstName}, <br /> <br /> Sentence 1. Sentence 2. <br /> <br /> Sentence 3. Sentence 4.", "Hi there <br /><br /> First line. Second line. <br /> Done.", "No matching pattern here" ]; const processedArray = htmlArray.map(str => { const match = str.match(flexibleRegex); // Return extracted content if matched, otherwise return the original string return match ? match[1].trim() : str; }); console.log(processedArray);
内容的提问来源于stack exchange,提问作者Bryce

