I wanted to capture the string between braces after specific selector. For example I have string like:
<div id="text-module-container-eb7272147" class="text-module-container"><style>#123-module-container-eb7272147 p{text-color:#211E22;bgcolor:test;} #text-module-container-eb7272147 p{color:#211E1E;} #text-module-container-eb7272147 p{color:#123444;} </style>
And now if I give selector #123-module-container-eb7272147 p
it should return text-color:#211E22;bgcolor:test;
I am able to get data between the braces but not with specific selector. This is tried code https://regex101.com/r/AESL8q/1
My Answer:
javascript
const str = "This is a test {string} to capture {braces} after specific {word}";
const word = "specific";
const regex = new RegExp(`${word}\\s*\\{([^}]+)\\}`, "g");
let matches = str.match(regex);
if (matches) {
matches = matches.map(match => match.replace(`${word} {`, "").replace("}", ""));
console.log(matches);
} else {
console.log("No matches found");
}
Output:
["word"]
Rate this post
3 of 5 based on 8737 votesComments