[Solved] split html text by some special tags [closed]


Try split join:

"some text <br />".split("<br />").join("");

if you have variable tags you may should try something like this:

var tagString = "someText<div class="someClass"><b><h1>someText<h1><br /></b></div>";
var noTagString = "";
var lastIndex = 0;
var dontRemove = ["<b>", "</b>"];
// iterate over the tagged text
for(var i = 0; i < tagString.length; i++){
   // check for '>'
   if(tagString[i] === "<"){
      // if '<' is found
      noTagString += tagString.substring(lastIndex, i);
      // take the left over
      var leftOver = tagString.substr(i, tagString.length);
      var goOn = false;
      // check for tags to keep
      for(var k = 0; k < dontRemove.length; k++){
         if(leftOver.startsWith(dontRemove[k])){
            goOn = true;
            break;
         }
      }
      if (goOn){
         // we found a tag we want to keep so go on
         continue;
      }
      // iterate over the left over
      for(var j = 0; j < leftOver.length; j++){
         // if closing tag is found
         if(leftOver[j] === ">"){
            // update i and last index
            i = i + j;
            lastIndex = i + 1;
            break;
         }
       }
    }
 }

this is not tested to well but maybe it points you in the right direction.
Put the tags you want to keep in the dontRemove array.

2

solved split html text by some special tags [closed]