[Regex] Solving this regex

The text is

--margin:20px
--margin-top:20px
---margin:20px;
---margin-top:10px
--margin-:290px
--margin:1px
--margin:20px-f
--margin:-20px;;
--margin:20px-
--margin-bottom:10px--margin-top:20px

I need to select everything as --key:value whatever key is and whatever value is

Morning, your question doesn’t really make much sense. Is this an assignment or something? Could you elaborate on what you are trying to accomplish.

what have you tried?

it’s not that the RegExp is awfully complicated …

Hi!

hmm, i will pickup the first example to view it:

var string = "--margin:20px"
var regex = ???;

while ((cap = regex.exex(string)) {
     // cap should be { 0: "--margin:20px", 1: "margin", 2: "20px" }
}

Something like this?

var string = "--margin:20px"
var re = /\-\-(.*?):(\d+px)/;
console.log(string.match(re));

// ["--margin:20px", "margin", "20px", index: 0, input: "--margin:20px"]

Please re-read the main post, you completely misunderstood it.

lol, oh dear!

Could you then maybe rephrase the question and give a more extensive example of the output you are expecting.

2 Likes

Almost made it an array…

var string = “–margin:20px\n”+
“–margin-top:20px\n”+
“—margin:20px;\n”+
“—margin-top:10px\n”+
“–margin-:290px\n”+
“–margin:1px\n”+
“–margin:20px-f\n”+
“–margin:-20px;;\n”+
“–margin:20px-\n”+
“–margin-bottom:10px–margin-top:20px”;
var regexp = /(?:(-\w+(?:-?\w):-?\d+\w+(?:[-\w;])))+/g;
string.match(regexp);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.