gpt4 book ai didi

javascript - RegEx JS 从另一个字符串中提取字符串匹配条件

转载 作者:行者123 更新时间:2023-11-29 11:01:17 25 4
gpt4 key购买 nike

我有这种类型的字符串:

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?匹配rgbrgba\(.*?\)匹配后面的第一对括号,假设您没有嵌套括号:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com