【问题标题】:Parsing CSS background-image 2015解析 CSS 背景图像 2015
【发布时间】:2015-02-20 02:02:54
【问题描述】:

我正在寻找这个问题的最新答案Parsing CSS background-image

那里的答案似乎是正确的,但不幸的是它去掉了渐变中停止点的百分比值(请参阅我的评论以获取示例)

所以我希望有人可以帮助我的是一个更新的正则表达式,比如 joos 接受的答案,它不会从渐变中去除百分比停止点值......

或者也许还有其他方法?

【问题讨论】:

  • 我认为 CSS background-image 属性值不符合正则语言的标准,因此无法使用正则表达式成功(可靠且稳健地)解析。
  • 实际上看着它,我需要的只是将属性拆分为不在括号内的任何逗号,我只需要一组背景图像......

标签: javascript css regex background-image


【解决方案1】:

好的,我已经完成了这似乎工作正常-可能不需要使用 jQuery,但我会发布更新...

splitBgImage : function(string) {
        //split the background-image string into an array of characters
        //iterate over the array of characters adding each to the bgimg variable
        //if we encounter a ( then we increment brackets by one
        //if we encounter a ) then we decrement brackets by one
        //if we encounter a , then if brackets == 0 then we push the bgimg property to the bgimgs array and set the bgimg property back to ''
        //if we are at the end of the array add whats left to bgimgs minus the ';'
        var brackets = 0, bgimg = '', bgimgs = [], splitString = string.split(''), totalLength = jQuery(splitString).length;
        jQuery.each(splitString, function(index, char){
            if(char == '(')
                {
                brackets ++;
                }
            if(char == ')')
                {
                brackets -- ;
                }
            if(char == ',' && brackets == 0)
                {
                bgimgs.push(bgimg);
                bgimg = ''; 
                }
            else
                {
                if(char != ';')
                bgimg += char;  
                }
            if(index == totalLength-1)
                {
                bgimgs.push(bgimg); 
                }
        });
        return bgimgs;
    }

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2014-05-25
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多