3

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].

angry kiwi
  • 10,730
  • 26
  • 115
  • 161
  • See: https://stackoverflow.com/a/432503/5894241 You need to invoke `regex.exec` multiple times to get subsequent capturing groups. – Nisarg Shah Aug 14 '18 at 05:37

2 Answers2

2

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);
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
0
let arr = [];
match.input.replace(pattern, function(match, g1, g2) { arr.push(g1); });
console.log(arr);
Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34
  • 4
    While this code may answer the question, please explain what you have done to solve the problem. Code-only answers are usually not helpful to other readers, and tend to attract downvotes. See [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Jesse Aug 14 '18 at 07:02