Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript regexp to find the first AND longest match

I have a RegExp like the following simplified example:

var exp = /he|hell/;

When I run it on a string it will give me the first match, fx:

var str = "hello world";
var match = exp.exec(str);
// match contains ["he"];

I want the first and longest possible match, and by that i mean sorted by index, then length.

Since the expression is combined from an array of RegExp's, I am looking for a way to find the longest match without having to rewrite the regular expression.

Is that even possible?

If it isn't, I am looking for a way to easily analyze the expression, and arrange it in the proper order. But I can't figure out how since the expressions could be a lot more complex, fx:

var exp = /h..|hel*/
like image 605
Michael Andersen Avatar asked Jan 21 '10 13:01

Michael Andersen


People also ask

Which function returns the index of the first longest match of regex in string str?

match(str, regex) It returns the index of the first longest match of regex in string str. It returns 0 if no match found.

Which regex matches one or more digits?

+: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.

What does RegExp do in JavaScript?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.

How do I match a pattern in regex?

Using special characters For example, to match a single "a" followed by zero or more "b" s followed by "c" , you'd use the pattern /ab*c/ : the * after "b" means "0 or more occurrences of the preceding item."


2 Answers

How about /hell|he/ ?

like image 132
YOU Avatar answered Nov 04 '22 12:11

YOU


All regex implementations I know of will (try to) match characters/patterns from left to right and terminate whenever they find an over-all match.

In other words: if you want to make sure you get the longest possible match, you'll need to try all your patterns (separately), store all matches and then get the longest match from all possible matches.

like image 21
Bart Kiers Avatar answered Nov 04 '22 14:11

Bart Kiers