Regular Expression Negative Lookahead

I have a process that creates XML and inserts data within a element.  Some of the data that is inserted includes "&"s.  Since "&" can not be inserted into the XML without causing problems, I need a process to escape "&", but I do not want "&" escaped for the following, since they have already escaped.
&, >, <, ', ",
 
This is pretty easy process with a while and indexOf, but quite slow.  I believe a RegEx could provide the same functionality, but be more efficient.   My solution uses the following negative lookahead regular expression. 
&(?!amp;)(?!gt;)(?!lt;)(?!apos;)(?!quot;)
 
 
Resources
Regular-Expressions.Info (Lookahead and Lookbehind Zero-Width Assertions)
 
!!(This did not seem to work, but it did point me in the right direction)Regex to match against something that is not a specific substring (
example of not working (&([^(&)(>)(<)(')(")])) it was close but not perfect

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *