Search code examples
javascriptstring-parsing

Escape html, except when inside [code] [/code] tags


I'd like to parse a string and output it, doing the following:

  • escape html - eg:
    • return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  • except for text within [code] [/code] tags
  • work for multiple code tags within the string

How do I identify the string inside/outside the tags? I don't need to store the string pieces, just process them and output.

Many thanks


Solution

  • I have written an example below that splits the string at the [code][/code] tags and then escapes all html that is not between those tags, it then puts that array back into a string, using a join command.

    Do note that the program will have unexpected behaviour if one of the [code] tags is missing.

    string = "hello world<b>this is mohammad's amazing code:</b> [code]<br>mohammad<br>is<br>amazing[/code]<br>"
    tempString = string.split(/(\[code\])(.+)(\[\/code\])/g)
    
    var isCode = 0
    for (var i = 0; i < tempString.length; ++i) {
      if (tempString[i].match(/(\[code\])/g)) {
        //code begins here
        isCode = 1
      } else if (tempString[i].match(/(\[\/code\])/g)) {
        //code ends here
        isCode = 0
      }
      if (isCode == 0) {
        tempString[i]=tempString[i].replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
      }
    }
    string = tempString.join("")
    alert(string)
    

    This demo can also be played with on jsfiddle