function extractContentBetweenTags(str: string, tagName: string): string | null {
// Build a regex pattern using the specified tag name
const pattern: RegExp = new RegExp(`<${tagName}>(.*?)</${tagName}>`);
// Use regex to match the pattern
const match: RegExpExecArray | null = pattern.exec(str);
if (match) {
// Return the content inside the tags
return match[1];
}
// Return null if no match is found
return null;
}
const str: string = "abcd <tag>efg hijk lmnop</tag> qrst uvwxyz";
// Call the function to extract the substring
const content: string | null = extractContentBetweenTags(str, "tag");
if (content) {
console.log(`Extracted content: ${content}`);
} else {
console.log("No matching tags found.");
}
/*
run:
"Extracted content: efg hijk lmnop"
*/