javascript - Replacing nested quotes in a faulty string inside XML Attribute using Regular Expression -


i have faulty string inside xml file: summary="the name "rambo"."

i replace inner quotes " using regex output like:

summary="the name &quotrambo&quot."

this should work you, buddie!

var outer = '"the name "rambo"."'; var inner = outer.replace(/^"|"$/g, ''); var final = '"' + inner.replace(/"/g, '"') + '"'; // (string) => "the name "rambo"." 

edit: shortcut little bit, it's asymmetrical because javascript not support regexp lookbehind

var str = '"the name "rambo"."'; var final = '"' + str.substr(1).replace(/"(?!$)/g, '"'); // (string) => "the name "rambo"." 

edit 2: using str.slice looks can simpler!

var str = '"the name "rambo"."'; var final = '"' + str.slice(1, -1).replace(/"/g, '"') + '"'; // (string) => "the name "rambo"." 

Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -