angry kiwi
angry kiwi

Reputation: 11485

get all class name in css file

I got css file like this :

let string =

    .papa, .aka {
        color:red
    }
    .mama .ok {
        color:blue
    }

    div .chacha{
        transition: opacity 0.2s;
    } .sasa {
        background:url(home.png)
    }

I want to fetch all the class name in the file and put in array. Expected result :

[.papa, .aka, .mama, .ok, .chacha, .sasa]

My try :

let pattern = /(?:[\.]{1})([a-zA-Z_]+[\w-_]*)(?:[\s\.\{\>#\:]{1})/igm

let match = pattern.exec(string);

console.log(match);

I only got [.papa].

Upvotes: 4

Views: 1623

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 10111

You can use the string.match

let string =
`
    .papa, .aka {
        color:red
    }
    .mama .ok {
        color:blue
    }

    div .chacha {
        transition: opacity 0.2s;
    } .sasa {
        background:url(home.png)
    }
`
var arr = string.match(/(?:[\.]{1})([a-zA-Z_]+[\w-_]*)(?:[\s\.\,\{\>#\:]{0})/igm);
console.log(arr);

Upvotes: 2

Shiv Kumar Baghel
Shiv Kumar Baghel

Reputation: 2480

let arr = [];
match.input.replace(pattern, function(match, g1, g2) { arr.push(g1); });
console.log(arr);

Upvotes: 0

Related Questions