JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id="editor" cols="30" rows="10">console.log('hello world')
some text
zzz "quotes in 'quotes' here" fff
escaped quote doesn\'t counts
</textarea>
<pre id="display"></pre>
CSS
#editor {
width: 100%;
height: 50vh;
background: white;
}
JavaScript
function analyzeThis(){
var quotedRanges = [], str = editor.value, inQutes = false, prev = ' ', char, quote, range;
for (let i = 0; i<str.length; i++) {
char = str[i]
if (!inQutes && char.match(/['"]/) && prev !== '\\') {
inQutes = true
quote = char
range = [i]
} else if (inQutes && char === quote && prev !== '\\') {
inQutes = false
range.push(i)
quotedRanges.push(range)
}
prev = char
}
displayRanges(quotedRanges)
}
analyzeThis()
editor.oninput = analyzeThis
function displayRanges(quotedRanges){
display.innerHTML = quotedRanges.map(range => {
let [start,end] = range
return range.toString() + ' # ' + editor.value.slice(start+1, end) + '\n'
}).join('')
}