简体   繁体   English

正则表达式以匹配基本媒体查询

[英]Regex to match basic media query

I am struggling to get my regular expression to match the following string: 我正在努力获取正则表达式以匹配以下字符串:

(max-width: 320px) 280px

I would like my regex to split the string into two parts like so: 我希望我的正则表达式将字符串分成两部分,如下所示:

console.log(parts[0]) // (max-width: 320px)
console.log(parts[1]) // 280px

This is what I currently have: 这是我目前拥有的:

(\(.+\))\s?(0-9a-z)+

I haven't written a regular expression in a while so I am a little rusty but for some reason, I cannot get it to match the final part ie 280px . 我有一段时间没有写正则表达式了,所以有点生疏了,但是由于某种原因,我无法使其与最终部分(即280px匹配。

Can anyone advise what I am doing wrong? 谁能告诉我我在做什么错?

Additional Information 附加信息

I do not need anything complex, the purpose of this regex is to match media queries used in the sizes attribute of the an img element. 我不需要任何复杂的东西,此正则表达式的目的是匹配img元素的sizes属性中使用的媒体查询。

As far as I can see, it simply needs to do the following: 据我所知,它只需要执行以下操作:

  1. Match anything within the brackets (including the brackets) 匹配方括号内的所有内容(包括方括号)
  2. Match an optional space 匹配可选空间
  3. Match the ending alphanumeric value (can be px/em/vw) 匹配结尾的字母数字值(可以是px / em / vw)

My guess is that you expression is just fine, maybe we'd just add a char class to it: 我的猜测是您的表达很好,也许我们只需向其添加一个char类:

(\(.+\))\s*([0-9a-z]+)

The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it. 如果您想探索/简化/修改该表达式,请在此演示的右上方面板中进行解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM