I am writing a PHP Syntax Highlighter code in JavaScript. Here is what I have tried:
<html>
<head>
<title>Untitled 1</title>
<script>
function showContents(text) {
var string = /\"[a-z0-9A-Z!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*\"/i;
var aVariable = /\$([a-zA-Z0-9_\x7f-\xff]*)/i;
var output = text;
if(aVariable.test(output)==true)
{
output = output.replace(aVariable.exec(output)[0],"<font color='blue'>"+aVariable.exec(output)[0]+"</font>");
document.getElementById("UserInput").innerHTML = output;
}
if(string.test(output)==true)
{
output = output.replace(string.exec(output)[0],"<font color='red'>"+string.exec(output)[0]+"</font>");
document.getElementById("UserInput").innerHTML = output;
}
else
{
document.getElementById("UserInput").innerHTML = output;
}
document.write(output);
}
</script>
</head>
<body>
<div id="UserInput"></div>
<form>
<textarea onkeyup="showContents(this.value)"></textarea></form>
</body>
</html>
The above script is working fine for inputs like:
$name="ABC"
However, if I try inputs like:
$name="ABC";$name2="CDE"
The code is highlighting only the first instance (ie $name="ABC"
). Changing the modifier to global is not giving output itself.