Shrikanth Buds Shrikanth Buds - 5 months ago 11x
Javascript Question

How to replace text between certain characters in javascript

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"';


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

Any help would be appreciated..:-D


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