-1

I am currently working on some server side code to parse HTML. I have following html string:

<div class="rte-style-maintainer rte-pre-wrap" style="white-space: pre-wrap; font-size: small; >This is a test for link {<a spellcheck="false" destination="rte:bind" data-destination="rte:bind">ABCD 156782053 </a>}</div>

And want to replace the content inside the bracket {} with

<a href="ABCD 156782053">ABCD 156782053 </a>

I am new to the regular expression. How can I get the "ABCD 156782053" from the bracket using regular expression? And replace the content inside bracket with new value?

Thanks

Terry Zhang
  • 4,541
  • 8
  • 23
  • 29
  • 2
    Your HTML is invalid due to a missing closing quote. Secondly, [don't parse HTML with regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) if you can help it. What specifically about the `{}` area is special? In other words, is this the only string you'll ever need to parse, or could it be in any format? Can the `{}` contain arbitrarily nested HTML structures? More detail is needed, and please show your attempt at solving this. Thanks. – ggorlen May 07 '20 at 02:22
  • If you haven't considered it, there are some lightweight js libraries with builtin support for templating like underscore or mustache. – xdhmoore May 07 '20 at 03:01

1 Answers1

0

First of all, your HTML syntax is wrong. Some closing quotes are missing.

Anyway, the syntax of this expression is that:

/{(.*?)}/g

Here is the test link: https://regexr.com/542vj

sundowatch
  • 3,012
  • 3
  • 38
  • 66