Reputation: 369
I'm trying to figure out how to construct a regex that can match any text inside curly braces, but also removes the curly braces on replacing.
Have the following strings/scenarios:
<span>Some text before {can be any text}<span>
<span>{can be any text} some text after<span>
<span>Some text before {can be any text} and some text after<span>
What I want, is replacing everything inside the curly braces including the braces itself and replace it with an HTML a tag.
What I have tried:
let link = '<span>Some text {some text inside}</span>'
link = link.replace('\{([^}]+)\}', `<a href=${link} target="_blank" key="anchor">some text inside</a>`)
console.log(link)
if I console the link out, it still has the curly braces plus content inside, so not sure whats wrong.
Upvotes: 0
Views: 897
Reputation: 173602
Regular expressions should start and end with /
instead of the normal string delimiters.
let link = 'https://example.org'
let content = '<span>Some text {some text inside}</span>'
content = content.replace(/\{([^}]+)\}/, `<a href="${link}" target="_blank" key="anchor">some text inside</a>`)
console.log(content)
Upvotes: 1