Based on RFC-7231 at https://tools.ietf.org/html/rfc7231#section-5.3.2, each media type also accepts optional key=value
parameters besides the q=0.7
. Here is a more comprehensive test case to covers this:
text/*, text/plain, text/plain;format=flowed, text/html;level=1, text/html;level=2;q=0.4, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.1
This regex tests for RFC compliance of the Accept
value:
/^(?!.*[():<>?@[\]{}x00-x08x0a-x1fx7f])s*[^/]+/[^,;]+(;s*[^=]+(?<!q)=[^,;]+)*(;s*q=[01](.d{1,3})?)?(,s*[^/]+/[^,;]+(;s*[^=]+(?<!q)=[^,;]+)*(;s*q=[01](.d{1,3})?)?)*s*$/
Same over multiple lines for readability (not a valid regex) :
/^(?!.*[():<>?@[\]{}x00-x08x0a-x1fx7f])
s*[^/]+/[^,;]+(;s*[^=]+(?<!q)=[^,;]+)*(;s*q=[01](.d{1,3})?)?
(,s*[^/]+/[^,;]+(;s*[^=]+(?<!q)=[^,;]+)*(;s*q=[01](.d{1,3})?)?)*
s*$/
Explanation:
^
- anchor to start of string
(?!.*[():<>?@[\]{}x00-x08x0a-x1fx7f])
- negative lookahead that tests for any of the invalid characters: greedy .*
match up to [...]
character class
s*
- scan over optional whitespace
[^/]+/[^,;]+
- scan over 1+ chars, a /
, and anything up to ,
or ;
, e.g. a media type, such as text/plain
(
... )*
- scan over optional key=value
pattern, zero to multiple times:
;s*
- scan over ;
separator, and optional whitespace
[^=]+(?<!q)
- scan over anything up to =
, but not a q
=
- scan over =
[^,;]+
- scan over anything up to ,
or ;
(
... )?
- scan over optional q=1
or q=0.001
pattern:
;s*
- scan over ;
separator, and optional whitespace
q=[01]
- scan over q=0
or q=1
(.d{1,3})?
- followed by optional .
and 1 to 3 digits (based on RFC)
- the first media type is covered up to this point
(
... )*
- scan over optional (zero to multiple) additional media types:
,s*
- scan over ,
separator, and optional whitespace
- followed by same pattern as above to: scan for media type, scan for optional
key=value
patterns separated by ;
, scan for optional q=...
pattern
s*
- scan over optional whitespace
$
- anchor string at the end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…