I have a string which contains a markup. Inside the markup there is an identifier/or substring which I want to pick which would look like this
ghost://{schema_name}/{id}
as an example
ghost://blogging/232323
Note: if required, we can change the identifier to something we can find more easily in our string.
Consider this as a string
const somesting = `
<h1> Random Heading </h1>
<p> Random Text </p>
<p> a href="ghost://blogging/232323"> Text I want to pick </a> </p>
`
Here, I want to pick ghost://blogging/232323 and change it to an actual link.
From googling, I found that I can achieve this by using grammar but I am not sure how to write grammar/regular expression here (or any other way)
Would so appreciate if anyone could help me out.
Based on the answer by CertainPerformance, I did this but unfortunately this isn't working
const somesting = `
<h1> Random Heading </h1>
<p> Random Text </p>
<p> a href="ghost://xman-article/HPHvLTH06YUMfdMnokOl/spaceyfi-product-launch"> Text I want to pick </a> </p>
`;
const replaced = somesting.replace(
/ghost:\/\/(\w+)\/(\d+)/g,
(_, schema_name, id, slug) => `/${schema_name}/${id}/${slug}`
);
console.log(replaced)