开发者

Regular Expression to match given word in last five words of pipe-delimited string

开发者 https://www.devze.com 2023-04-05 23:10 出处:网络
Say we have a string blue|blue|green|blue|blue|yellow|yellow|blue|yellow|yellow| And we want to figure out whether the word \"yellow\" occurs in the last 5 words of the string, specifically by retu

Say we have a string

blue|blue|green|blue|blue|yellow|yellow|blue|yellow|yellow|

And we want to figure out whether the word "yellow" occurs in the last 5 words of the string, specifically by returning a capture group containing these occurences if any.

Is there a way to do that with a regex?

Update: I'm feeding a regex engine some rules. For various reasons I'm trying to work with the engine ra开发者_开发百科ther than go outside it, which would be my last resort.


/\b(yellow)\|(?=(?:\w+\|){0,4}$)/g

This will return one hit for each yellow| that's followed by fewer than five words (per your definition of "word"). This assumes the sequence always ends with a pipe; if that's not the case, you might want to change it to:

/\b(yellow)(?=(?:\|\w+){0,4}\|?$)/g

EDIT (in response to comment): The definition of a "word" in this solution is arbitrary, and doesn't really correspond to real-world usage. To allow for hyphenated words like "real-world" you could use this:

/\b(yellow)\|(?=(?:\w+(?:-\w+)*\|){0,4}$)/g

...or, for this particular job, you could define a word as one or more of any characters except pipes:

/\b(yellow)\|(?=(?:[^|]+\|){0,4}$)/g


No need to use a Regex for such a simple thing.

Simply split on the pipe, and check with indexOf:

var group = 'blue|blue|green|blue|blue|yellow|yellow|blue|yellow|yellow';

if ( group.split('|').slice(-5).indexOf('yellow') == -1 ) {
    alert('Not there :(');
} else {
    alert('Found!!!');
}

Note: indexOf is not natively supported in IE < 9, but support for it can be added very easily.


Can't think of a way to do this with a single regular expression, but you can form one for each of the last five positions and sum the matches.

var string = "blue|blue|green|blue|blue|yellow|yellow|blue|yellow|yellow|";

var regexes = [];
regexes.push(/(yellow)\|[^|]+\|[^|]+\|[^|]+\|[^|]+\|$/);
regexes.push(/(yellow)\|[^|]+\|[^|]+\|[^|]+\|$/);
regexes.push(/(yellow)\|[^|]+\|[^|]+\|$/);
regexes.push(/(yellow)\|[^|]+\|$/);
regexes.push(/(yellow)\|$/);

var count = 0;
var regex;
while (regex = regexes.shift()) {
  if (string.match(regex)) {
    count++;
  }
}

console.log(count);

Should find four matches.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号