The title of my question is a bit complicated, I know, but here is basically what I want to do:
Say I have this piece of text:
[table]
[tr]
[td]test str 1[/td]
[td]test str 2[/td]
[/tr]
[/table]
Would there be a regex, that allows me to find:
- A string that is between the [td] and [/td] tag
- Of which the entire part from [td] to [/td] is itself between the [table] and [/table] tags
- And the text that is between the [table] and [td] tags can't contain the [/table] tag
- And the text that is between the [/td] and [/table] tags can't contain the
[table] tag
It might sound obvious, but it should be a safe regex because this regex will be used to handle user input, and if a user were to enter a [td] outside of a table (all the tags are converted to html), it could affect the tables used for the layout of my site's page.
So it should match "test str 1" first, and on the next go "test str 2", but only if that string is within the td tags, which should in turn be within the table tags between which may not be another table tag.
This is as close as I've gotten:
/\[table(.*?)\]((?!\[\/table\]).*?)\[td(.*?)\](.*?)\[\/td\]((?!\[table(.*?)\]).*?)\[\/table\]/si
But I think I'm missing something in the parts where the table tags should not be there, so between the table and td tags.