I'd like to parse a string and output it, doing the following:
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
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
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, '&').replace(/</g, '<').replace(/>/g, '>');
}
}
string = tempString.join("")
alert(string)
This demo can also be played with on jsfiddle