regex - Javascript regular expression to find double quotes between certain characters -
i'm trying create string can parsed json. string dynamically created based on content in cms. content contain html markup double quotes, confuses json parser. so, need replace double quotes in html "
without replacing double quotes part of json structure. idea wrap the html inside markers, use identify between these markers quotes want replace. instance string want parse json this...
str = '{"key1":"xxx<div id="divid"></div>yyy", "key2":"xxx<div id="divid"></div>yyy"}';
so, want replace every double quote between xxx , yyy "
. like...
str = str.replace(/xxx(")yyy/g, '"');
hope made sense. suggestions.
given stack overflow's "we don't homework" principles, don't think i'm going work through whole solution, can give pointers in half-finished code.
var xysearch = /this regex should find text between xxx...yyy/g; // note g @ end! that's important var result; var doublequoteindices = []; // note single-equals. avoid them when possible inside "condition" statement, // here sort of makes sense. while (result = xysearch.exec(str)) { var block = result[0]; // inside of block, find index in str of each double-quote, , add // doublequoteindices. need result.lastindex absolute position. } // loop backwards through str (so left-side replacements don't change right-side indexes) // replace characters @ each doublequoteindices appropriate html.
i kind of find great regex's patterns, having programming language of work best solution.
Comments
Post a Comment