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?

5
  • 1
    What do you mean by disable the capture on the beginning of a Regex? Commented Sep 5, 2016 at 10:51
  • Do you have to use String#match rather than an exec loop? Commented Sep 5, 2016 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". Commented Sep 5, 2016 at 10:55
  • match doesn't work the way you want it to with /g and multiple matches, You will have to use stackoverflow.com/a/10939979/3355076 Commented Sep 5, 2016 at 10:56
  • .match( /(^|[^\])((?!\?)/,"(?:" Commented Sep 5, 2016 at 10:58

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

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()))

4 Comments

Answers shouldn't be questions. Also, say what you changed, and why.
"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"]
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.
Thanks T.JCrowder for your comments. Edited this answer itself. I hope this answers AntiMuffin's query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.