javascript - Replacing XML attribute values using regex -


i have svg stored in string. part of looks like:

var str = '<svg version="1.1" width="1400px">'; 

i'm looking solution replace value of given property. right now, if wanted change width, i'm doing following:

var prop = "width"; var newvalue = "100px"; var regex = new regexp(prop + '="[^"]*'); str = str.replace(regex, prop + '="' + newvalue); 

is there way single regular expression dont have build string afterwards?

or put differently, regular expression select text between quotes when preceded specific string?

fill="**howcaniselectthistext?**"

better use buffer , treat real html object;

jsfiddle

html

<div id="hidden"></div> 

css

#hidden {     display: none;  } 

javascript

var hidden = document.getelementbyid('hidden'); var str = '<svg version="1.1" id="layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" fill="#ffffff"  width="1400px" height="180px" viewbox="0 0 1400 180" enable-background="new 0 0 1400 180" xml:space="preserve">'; hidden.innerhtml = str;  var prop = "width"; var newvalue = "100px";  var svgobject = hidden.getelementsbytagname('svg')[0]; svgobject.setattribute(prop, newvalue)  console.log(svgobject.getattribute('width'));  // done str = hidden.innerhtml;  // when you're done, empty hidden buffer hidden.innerhtml = ''; 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -