27

I have been messed up with regex.. I found it difficult for me..I have seen a code like:

function myFunction() {
   var str = "Is this all there is";
   var patt1 = /is(?= all)/;
   var result = str.match(patt1);
   document.getElementById("demo").innerHTML = result;
}

When i run this code it gave me the output is.

But when i add like /is(?=there)/ it didnt output anything. I am new to regular expression ..hope you guys can help in in understanding positive lookahead in regex..I have followed many tutorials it didnt helped me.

Hope you guys can help me out. Thanks!

hwnd
  • 69,796
  • 4
  • 95
  • 132
user3718914
  • 285
  • 1
  • 3
  • 8

2 Answers2

40

The regex is(?= all) matches the letters is, but only if they are immediately followed by the letters all

Likewise, the regex is(?=there) matches the letters is, but only if they are immediately followed by the letters there

If you combined the two in is(?= all)(?=there), you are trying to match the letters is, but only if they are immediately followed both by the letters all AND the letters there at the same time... which is not possible.

If you want to match the letters is, but only if they are immediately followed either by the letters all or the letters there, then you can use:

is(?= all|there)

If, on the other hand, you want to match the letters is, but only if they are immediately followed by the letters all there, then you can just use:

is(?= all there)

What if I want is to be followed by all and there, but anywhere in the string?

Then you can use something like is(?=.* all)(?=.*there)

The key to understanding lookahead

The key to lookarounds is to understand that the lookahead is an assertion that checks that something follows, or precedes at a specific position in the string. That is why I bolded immediately. The following article should dispel any confusion.

Reference

Mastering Lookahead and Lookbehind

Liggliluff
  • 706
  • 1
  • 6
  • 22
zx81
  • 41,100
  • 9
  • 89
  • 105
  • am sorry still i didnt get "but only if they are followed by the letters all"..how can i know if its folowed or not – user3718914 Jun 15 '14 at 05:58
  • @user3718914 Lookarounds are a bit confusing at the start. I highly recommend you read that article I linked, as it's very clear and will probably do a better job than us trying to write another article here. :) – zx81 Jun 15 '14 at 06:02
  • @user3718914 Also in the answer I added the bold words **immediately** I think it will help. – zx81 Jun 15 '14 at 06:04
  • yeah i have accepted the answer ..is this depends on the position of the string too ??..i mean in the case of lookahead – user3718914 Jun 15 '14 at 10:35
  • @user3718914 `is this depends on the position of the string too` Can you please explain what you mean? Not sure I understand your question. – zx81 Jun 15 '14 at 10:45
  • @user3718914 The lookahead says "we can match this" exactly at the position of the lookahead, i.e. after the `is`. If you want it to look ahead further down the string, you include that in the lookahead for instance with a `.*`. So in `is(=.*Bob)`, the lookahead asserts "at this exact position in the string, i.e. after the `is`, I am able to match `.*`, i.e. any characters, then `Bob`. So you are able to look further down the string, but you stay in the same place in the string, after the `is`. Adding the `.*` is like using binoculars.Hey I'm shutting down for the day, hope this is helpful! :) – zx81 Jun 15 '14 at 10:55
9

The Positive Lookahead fails for the fact that there does not immediately follow is.

is(?=there) # matches is when immediately followed by there

To match is if there follows somewhere in the string, you would do:

is(?=.*there) 

Explanation:

is          # 'is'
(?=         # look ahead to see if there is:
  .*        #   any character except \n (0 or more times)
  there     #   'there'
)           # end of look-ahead

See Demo

A detailed tutorial I recommend: How to use Lookaheads and Lookbehinds in your Regular Expressions

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • am sorry still i didnt get "but only if they are followed by the letters all"..how can i know if its folowed or not – user3718914 Jun 15 '14 at 05:58