I have a program that allows a user to type JavaScript in a textbox, and it executes in an HTML viewer or iframe. There is a drop down with options such as “Insert Image”, which inserts
var $'Image Name' = document.createElement('img');
$'Image Name'.src = $'Image URL';
$'Image Name'.style.position = 'absolute';
document.body.appendChild($'Image Name');
into the textbox. I want the user to select the option “Insert Image”, and have a dialog go through each $'', and ask for something to replace them with, so for the first $'Image Name', prompt the user for a variable name, and then replace("$'Image Name'", userText) so all the $'Image Name's get replaced and the user isn’t prompted for the same one again. Any ideas? I have this replace:
replace(/^\$\"|\'.$\"|\'/gi, function ($string) { return prompt($string);});
but it matches the quotes, not the text inside the quotes, and I don’t even know regex, if you can solve the regex, I can figure out the rest.
I guess you want something like this:
though I should mention that this will misbehave if a single-quoted string happens to end with a dollar-sign, or if a double-quoted string happens to contain
$', or whatnot; do you need to be able to handle these sorts of cases? (It’s not obvious from your question whether you completely control the contents of the textarea, or whether it’s something your users might be editing.)