askohen
01-08-2007, 11:45 AM
Hi. As an exercise for a beginner at JS, I would like to do the following: Convert a string of HTML to escaped HTML, with entities instead of brackets etc. My thinking is this:
1) Using reg expressions create a pattern that matches '<', '>', '&' BUT NOT any word beginning with '&' ';' and ending with ';'. --so that actual entities are not (re)converted to entities...
2) Use regex.exec(), get an array of all matches
3) Replace those matches with corresponding HTML entities.
4) Return the replacements back into the string.
I can loop through the matches using a while loop with re.exec(), and reassign each n element of the array with the entity, but I have no idea how to then return that back into the string. Am I on the right track, or is there an easier way? Thanks.
1) Using reg expressions create a pattern that matches '<', '>', '&' BUT NOT any word beginning with '&' ';' and ending with ';'. --so that actual entities are not (re)converted to entities...
2) Use regex.exec(), get an array of all matches
3) Replace those matches with corresponding HTML entities.
4) Return the replacements back into the string.
I can loop through the matches using a while loop with re.exec(), and reassign each n element of the array with the entity, but I have no idea how to then return that back into the string. Am I on the right track, or is there an easier way? Thanks.