If I have string like
var a = '<?xml version="1.0" encoding="Windows-1251"?> .. encoding="utf-8"';
how to extract only value of first occurance of encoding attribute only inside xml tag with regexp? So in result would be
Windows-1251
?
If I have string like
var a = '<?xml version="1.0" encoding="Windows-1251"?> .. encoding="utf-8"';
how to extract only value of first occurance of encoding attribute only inside xml tag with regexp? So in result would be
Windows-1251
?
If you really want to use a regex, as is your question, you may use
var val = a.match(/<\?xml[^>]+\s+encoding="([^"]*)"/)[1];
Note that usually, parsing the string, especially in a browser, is the simplest solution.
Because if you want to account for all cases, for example the possibility that you have encoding=
at the end of an attribute value, then it starts to be nasty :
var val = a.match(/<\?xml(\s+[^>="]+="[^"]+")*\s+encoding="([^"]*)"/)[1];
Note that if you're not sure the attribute is present, you should check that the returned array isn't null before taking the element at index 1.