search

Home  >  Q&A  >  body text

Python tutorial: Splitting method to solve overlapping position problem

<p>My goal is to split the overlapping positions for use in HTML. For example, I have this text: </p> <blockquote> <p>One Two Three</p> </blockquote> <p>I want the second and third words to be bold, and the first two words to be italicized. We get this result:</p> <pre class="brush:php;toolbar:false;">[ { "start": 4, "end": 13, "markup": "BOLD" }, { "start": 0, "end": 7, "markup": "ITALIC" } ]</pre> <p>If we wish to apply HTML tags by position, we get this result: </p> <pre class="brush:php;toolbar:false;"><bold>One <em>Two</bold> Three</em></pre> <p>See the overlap? <code><em></code>the opening tag is interrupted by the <code></bold></code>closing tag. It should look like this:</p> <pre class="brush:php;toolbar:false;"><bold>one<em>two</em></bold><em>three</em></ pre> <p>and location: </p> <pre class="brush:php;toolbar:false;">[ { "start": 4, "end": 7, "markup": "BOLD" }, { "start": 7, "end": 13, "markup": "BOLD" }, { "start": 0, "end": 7, "markup": "ITALIC" } ]</pre> <p>P.S. Don't worry about the original position changing after applying the markup. I wrote a good solution. </p> <p>I tried to implement some solutions but it seems to be a difficult task for me. </p>
P粉063039990P粉063039990473 days ago469

reply all(1)I'll reply

  • P粉651109397

    P粉6511093972023-08-14 13:17:00

    You can use the Javascript DomParser API to automatically fix the HTML structure

    const res = (new DOMParser())
        .parseFromString(
            "<bold>One <em>two</bold> three</em>",
            "text/html"
        );
    console.log(res.body.innerHTML)

    reply
    0
  • Cancelreply