0

I want to capture only a area of the Match part. I have following String: " type1-class1 type1-class2 type2-class4 type2-type1-class3 " and i want following result ["class1", "class2"]. I am searching the class by the type. I have tried following:

console.log(" type1-class1 type1-class2 type2-class4 type2-type1-class3 ".match(/\btype1-.*?(?= )/g))
// Result: ["type1-class1", "type1-class2", "type1-class3"]

Furthermore i have tried following:

console.log(" type1-class1 type1-class2 type2-class4 type2-type1-class3 ".match(/(?= type1-).*?(?= )/g));
// Result: ["", ""]

How can i disable the capture on the beginning of a Regex?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
kpalatzky
  • 1,213
  • 1
  • 11
  • 26
  • 1
    What do you mean by `disable the capture on the beginning of a Regex`? – evolutionxbox Sep 05 '16 at 10:51
  • Do you *have* to use `String#match` rather than an `exec` loop? – T.J. Crowder Sep 05 '16 at 10:52
  • Because i can use (?= ...) to match but not capture, this is only working on the center/end of a match but not at the beginning. However, i want to match a string and only to get a part of it. As i mentioned above i want to match `" type1-class1 "` and get `"class1"` and the same on `" type1-class2 "` and get `"class2"`. – kpalatzky Sep 05 '16 at 10:55
  • match doesn't work the way you want it to with /g and multiple matches, You will have to use http://stackoverflow.com/a/10939979/3355076 – Christopher Reid Sep 05 '16 at 10:56
  • .match( /(^|[^\\])\((?!\?)/,"(?:" – khakishoiab Sep 05 '16 at 10:58

2 Answers2

4

I'd use an exec loop. The regular expression is /(?:^|\s)type1-([^ ]+)/g, e.g., "Following a type-1 that's at the beginning of the string or just after whitespace, capture everything until the next whitespace or end of string. (With your string as given, we don't need the alternation with ^, but I wanted to allow for "type1-class1" at beginning of string.) We then collect the captures in a loop:

var rex = /(?:^|\s)type1-([^ ]+)/g;
var str = " type1-class1 type1-class2 type2-class4 type2-type1-class3 ";
var classes = [];
var m;
while ((m = rex.exec(str)) != null) {
  classes.push(m[1]);
}
console.log(classes);
kpalatzky
  • 1,213
  • 1
  • 11
  • 26
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

is this what you are expecting?

console.log(" type1-class1 type1-class2 type2-class4 type2-type1-class3 ".match(/ type1-.*?(?= )/g).map(x=>x.replace(/type1-/g,"").trim()))
Karpak
  • 1,927
  • 1
  • 15
  • 16
  • Answers shouldn't be questions. Also, say *what* you changed, and *why*. – T.J. Crowder Sep 05 '16 at 11:05
  • *"is this what you are expecting?"* Clearly not, as the OP clearly says he/she wants `["class1", "class2"]` but the above returns `["class1", "class2", "class3"]` – T.J. Crowder Sep 05 '16 at 11:06
  • But the idea of post-processing with `map` isn't a bad one, might be worth fleshing it out to make this a useful answer. – T.J. Crowder Sep 05 '16 at 11:09
  • Thanks T.JCrowder for your comments. Edited this answer itself. I hope this answers AntiMuffin's query. – Karpak Sep 05 '16 at 11:10