I would do it using a capturing split() on /([#"])/, which splits<div><br></div><div>Before the string "Inside the string" # comment</div><div><br></div><div>into </div><div>[ 'Before the string',</div>
<div> '"',</div><div> 'Inside the string',</div><div> '"',</div><div> '#',</div><div> 'comment',</div><div>]</div><div><br></div><div>Before entering the array, you are obviously not in a string, not in a comment ... as you process elements, you find where you are. If you want to detect more 'separator characters', they're simple to add, and simple to handle depending on whether you are in a string, comment or other/</div>
<div><br></div><div>Tom</div>