javascript - Matching charset of HTTP Header Content-Type -
in javascript, want "charset" attribute of http header field name 'content-type'
the regex i've seen far has been like:
var charset = (/^charset=(.+)/im).exec(contenttype)[1]; with contenttype contain informations of content-type http header.
but in testing, matched result 'null'
edit: follow response @andris leduskrasts,
var ctype = 'text/html; charset=utf-8'; var charset = new regexp('charset=.*?(?=$|\s|\;|\")').exec(ctype); system.stdout.writeline(charset); i 'charset=utf-8'. idea 'utf-8'. ?
i experienced same problem.
if need extract charset value arbitrary content-type header (which permits characters after charset assignment per rfc1341) can use following js regexp:
var re = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i; this works because matched group starts after = , excludes possible endings of charset specification given in link; namely ()<>@,;:\"/[]?.=, spaces, , (implicitly) end-of-string.
since charset optional, can set appropriate value like:
var charset = re.test(ctype) ? re.exec(ctype)[1] : 'utf8'; or other default.
Comments
Post a Comment