I am writing a system whereby everything visible to the user is translatable. This means that every paragraph, title, link text, tooltip etc is contained in a database table and I am using tags to get the data. For example [Tag 1] may be Login or it could be changed to something else – it’s definable by the company itself.
I have this working perfectly. I am overriding the Render method in the Page class to replace all these tags. I have a regex to match all tags in the format [tag x] where x is 1 or more numbers, i.e. the ID of the translation in the database; the regex I am using is @"\[(Text|Tag) ([0-9]*)].
The problem I am having is that, for example, if you have a tag within a tag – for example [tag 69] is defined as [tag 70][tag 71][tag 72] – when the text is inside a textarea it is still replaced, so tag 69 will appear as the other 3 put together. I’d like to prevent replacements on tags contained within textareas. My first thought was that this would be easy – but then I realised that if you had <textarea>[tag 1]</textarea>[tag 2]<textarea>[tag 3]</textarea>, tag 2 would not be replaced.
I have to admit I am no pro at regular expressions – I would like to be but I don’t have the time on my hands at the moment. Can anyone suggest a way that I could achieve what I’d like to achieve? Thanks in advance.
Regards,
Richard
So the question is how to match a
[Tag n]that isn’t enclosed inside a textarea? You can use a negative look-behind for that.The code:
Outputs: