我有这种类型的字符串:
url('img.png') rgba(123, 111, 23, 0.96) none repeat scroll 0% 0% / auto padding-box padding-box
url('img.png') rgb(123, 111, 23) none repeat scroll 0% 0% / auto padding-box padding-box
注意rgb/rgba的区别
我只需要提取 rgba(123, 111, 23, 0.96)
或 rgb(123, 111, 23)
本质上,如何选择以 rgb
开头并以 parenthesis )
结尾的字符串?
最佳答案
您可以使用 /rgba?\(.*?\)/
和 String.match
方法; rgba?
匹配rgb
或rgba
,\(.*?\)
匹配后面的第一对括号,假设您没有嵌套括号:
var samples = ["url('img.png') rgba(123, 111, 23, 0.96) none repeat scroll 0% 0% / auto padding-box padding-box",
"url('img.png') rgb(123, 111, 23) none repeat scroll 0% 0% / auto padding-box padding-box"]
console.log(
samples.map(s => s.match(/rgba?\(.*?\)/))
)
// if have more than one matches in the strings
console.log(
samples.map(s => s.match(/rgba?\(.*?\)/g))
)
关于javascript - RegEx JS 从另一个字符串中提取字符串匹配条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46650411/