Python tutorial: Splitting method to solve overlapping position problem
P粉063039990
P粉063039990 2023-08-13 15:52:41
0
1
498
<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粉063039990
P粉063039990

reply all(1)
P粉651109397

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)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template