Reputation: 21
I have lines that start with a number followed by a space, a key and then the value. I need only the value. The problem is that my regex returns the HOLE line instead of only the value.
lines example:
1 NAME Bob /Cox/
1 SEX M
1 FAMS @F1@
regex:
/^(?=1\s[\w]*\s)[\w\W\s]*/
Did i misunderstood how "positive lookahead" works?
Upvotes: 0
Views: 87
Reputation: 3446
var r,s='1 NAME Bob /Cox/\n1 SEX M\n1 FAMS @F1@()',m=[],
re=new RegExp('1 [A-Z]+ (.+)','g');
while(r=re.exec(s)){
m.push(r[1]);
}
// m=["Bob /Cox/", "M", "@F1@()"]
Or a more robust approach:
var r,s='1 NAME Bob /Cox/\n1 SEX M\n1 FAMS @F1@()',m=[],
re=new RegExp('1 ([A-Z]+) (.+)','g');
while(r=re.exec(s)){
var o={};
o[r[1]]=r[2];
m.push(o);
}
// m=[{NAME:"Bob /Cox/"},{SEX:"M"},{FAMS:"@F1@()"}]
Upvotes: 0
Reputation: 664494
Did i misunderstood how "positive lookahead" works?
Probably. Lookahead doesn't mean "jump over this" (and don't include it in the match), but "only match if this lookahead matches from here" (and don't include it in the match, just go on from where the lookahead started).
You would need lookbehind here, which is not supported by javascript. There are a few ways around it, the easiest would be to use capturing groups here:
var r = /^1\s([\w]*)\s([\w\W\s]*)/;
lines.split("\n").forEach(function(line) {
var m = line.match(r);
if (m) {
var key = m[1];
var value = m[2];
console.log(value);
}
});
Upvotes: 2
Reputation: 39365
The regex you are you are using it first checks using lookahead((?=1\s[\w]*\s)
) whether a certain pattern is available or not upfront. It doesn't mean that it is skipping these matches. Its only checking it's existence. Which means your next portion of regex still starts exactly after the start of string(^
).
For your particular this case you can use replace
instead of regex matching:
var input = '1 NAME Bob /Cox/\n1 SEX M\n1 FAMS @F1@';
input = input.replace(/^1\s[\w]*\s/mg, "");
Upvotes: 1