Substring text with HTML tags in Javascript

前端 未结 6 1077
小蘑菇
小蘑菇 2020-12-14 22:25

Do you have solution to substring text with HTML tags in Javascript?

For example:

var str = \'Lorem ipsum dolor sit         


        
6条回答
  •  时光说笑
    2020-12-14 23:05

    it is solution for single tags

    function subStrWithoutBreakingTags(str, start, length) {
        var countTags = 0;
        var returnString = "";
        var writeLetters = 0;
        while (!((writeLetters >= length) && (countTags == 0))) {
            var letter = str.charAt(start + writeLetters);
            if (letter == "<") {
                countTags++;
            }
            if (letter == ">") {
                countTags--;
            }
            returnString += letter;
            writeLetters++;
        }
        return returnString;
    }
    

提交回复
热议问题