Re: Checking equal number of <div> and </div> [message #171328 is a reply to message #171297] |
Thu, 30 December 2010 12:23 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
jwcarlton wrote:
> Thomas 'PointedEars' Lahn wrote:
>> jwcarlton wrote:
>>> Can you guys think of a good way for me to check a string to make sure
>>> there are an equal number of <div (.*)> and </div>? Then, either add
>>> or remove </div> tags as needed to make them match?
>>
>> A recursive RegEx to parse HTML has been posted (by me) and can be easily
>> found in the manual; whatever is not matched by it can be analyzed
>> further.
>>
>> However, you should (also) describe the problem you want to solve, not
>> (only) the problem with your approach for a solution, for the approach
>> might be completely wrong.
>
> It's for a message board, where I've implemented Rich HTML posting
> (contentEditable). It works fine except for when someone copy-and-
> pastes from a site that uses DIV tags for layout. In those cases, if
> the poster doesn't grab the entire page, then it's not uncommon for
> the opening or closing DIV tags to be missing.
In that case you should try the suggested approach:
1. See if there is any <div …> in the remaining text.
2. If there is, match the recursive RegExp, slightly adapted for
DIV elements, against that.
a. If it matches, the tags are balanced in the matched part.
Continue at 1.
3. See if there is any <div …> in the remaining text.
a. If yes, you have more <div …> than </div>. Append a </div> until
the RegExp matches the whole text.
b. If no, you have more </div> than <div …>. Either remove the </div>
at the end, or prepend a <div> at the start, until the RegExp
matches the whole text.
Please trim your quotes to the parts you are referring to,
which especially means: Usually DO NOT quote signatures. TIA.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
|
|
|