javascript - 解析 CSS 背景图像 2015

标签 javascript css regex background-image

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

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

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

或者也许还有其他方法?

最佳答案

好的,我已经完成了这似乎工作正常 - 可能不需要使用 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;
    }

关于javascript - 解析 CSS 背景图像 2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620520/

相关文章:

javascript - 如何在 JavaScript 中高效计算最近的二维点?

javascript - 根据大小将数字动态舍入到小数点后 n 位

javascript - 插入输入日期后我没有得到输出

java - 正则表达式帮助——清理空格——Java

javascript - 在完成加载之前获取 AJAX 响应的大小

javascript - 数组的声明

javascript - ng-mouseover/mouseleave 为每个列表项设置动画

html - 无法在我的菜单中将文本居中

Python正则exp匹配上标2

regex - 如何在 Perl 中替换连续且相同的字符?