Insert character at specific point but preserving tags?

后端 未结 2 886
無奈伤痛
無奈伤痛 2021-01-18 02:22

Update #2

Okay, more testing ensues. It looks like the code works fine when I use a faux spacer, but the regex eventually fails. Specifically, the following scenari

2条回答
  •  耶瑟儿~
    2021-01-18 02:55

    Well, I came up with a solution, rather straightforward as well.

    If .text looks like this:

    Line one Line two Line three

    With the exact markup and line breaks as above, then I can find each \n and replace it with a spacer element.

    if (textStr.indexOf("\n") >= 0) {
        textStr = textStr.replace(/\n/g, "\n");
    }
    

    This isn't versatile at all, and will fail if there are more than one line breaks, if the tags are different, etc. So, I encourage anyone who has a better method to answer the question! It can't be that hard, I figured it out.

提交回复
热议问题