What regex to match all occurrences of css urls?

后端 未结 6 2158
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-20 14:36

i have a css file with a bunch of background image urls:

.alert-01 {
  background: url(\'img/alert-01.jpg\') center no-repeat;
}
.alert-02 {
  background: url(\'         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 14:39

    Use the following regex:

    /\burl\([^)]+\)/gi
    

    There's no need to use anchors (^ and $) as they would restrict the match over whole string input and hence fail. To capture the image path separately so that you can use it easily (and avoid substring) when constructing your tags use:

    /\burl\('([^']+)'\)/gi
    

    This captures img/alert-xx.jpg in group one.

提交回复
热议问题