0

Im trying to do a code coloring program where certain keywords are replaced with colorized text. Until now im able to succeed at replacing certain keywords but im not able to replace the words between the quotes. for example i would like to replace.

var message='"im some text"';

with

var message='<span style="color:yellow">im some text</span>';

Any help would be appreciated..:-D

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Shrikantha Budya
  • 646
  • 1
  • 4
  • 15
  • This might give you an idea of what im trying to do.it is yet to do the replacing between the quotes though http://codepen.io/megatroncoder/pen/KzYGwY – Shrikantha Budya May 15 '16 at 03:26

1 Answers1

1

If you want to replace text between quotes in a string then use replace() with captured group regex and do something like this

var div = document.getElementById('div');
div.innerHTML = div.innerHTML.replace(/"([^"]+)"/g, '<span style="color:yellow">$1</span>');
<div id="div">abc "asasa" abc</div>

Regex explanation here

Regular expression visualization

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188