-3

I have this regular expression, which will do a positive look behind on the word google, and I have tested it for validity using the website regex101.com, which showed me that it works:

(?<=google).*

I have used it in JavaScript like so:

var regex = new RegExp('(?<=google).*');
    someString = "google everything!";

console.log(someString.match(regex)[0]);

The console log should return everything! with a space behind it, basically all characters excluding new lines after the word google.

I have also tried without the need of creating a new RegExp object like so:

var someString = "google everything!";
console.log(someString.match(/(?<=google).*/)[0]);

... but I get the same error:

Uncaught SyntaxError: Invalid regular expression: /(?<=google).*/: Invalid group

AGE
  • 3,752
  • 3
  • 38
  • 60
  • 2
    There is no lookbehind in JS. – elclanrs Mar 03 '16 at 18:00
  • so the regex is invalid because of JS? – AGE Mar 03 '16 at 18:02
  • 3
    No, the regex is OK, but JS does not "understand" it :) There are a lot of regex flavors, and some constructs are flavor-dependent. In your case, you just need to use a capturing group: [`google(.*)`](https://regex101.com/r/aI1dK0/1). Group 1 holds your data. – Wiktor Stribiżew Mar 03 '16 at 18:03
  • I see (that is unexpected) well then a simple string substring should do the job :) thanks! – AGE Mar 03 '16 at 18:04
  • 1
    FWIW discussions are currently underway about adding lookbehind support to JavaScript, but it'll probably be a while before anything lands. (I just tried to find a link to the current discussions, but failed.) – Jeremy Mar 03 '16 at 18:09
  • @JeremyBanks this is a very interesting topic I will look into it, I also just realized thanks to the accepted answer, it was a matter of using the wrong regex notation for JavaScript, I was basing mine from Java by mistake, see: [this answer](http://stackoverflow.com/a/5007710/1046690). – AGE Mar 03 '16 at 18:15
  • 1
    JavaScript is based on ECMAScript, if you care about regex. And is extremely crippled. –  Mar 03 '16 at 18:19

2 Answers2

3

Your regex seems incorrect. I'm not sure why you have <=. Secondly, you're looking for the second index, not first.

var regex = new RegExp('google(.*)'),
    someString = "google everything!",
    matches = someString.match(regex); // ['google everything', ' everything!']
    console.log(matches[1]); //  everything! (with space before it)
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • This is because I made the mistake of reading [this answer](http://stackoverflow.com/a/5007710/1046690) and not noticing that the language used was Java. Good catch! now all I need is to add a `trim()` to the `matches[1]` and I have what I need. Thanks a lot! – AGE Mar 03 '16 at 18:13
2

No lookbehind assertions in JS.

You can get around this by using a capture group around just the part
you want to get.

google(.*)

Link to try https://jsfiddle.net/vxcdk1kv/

  • 1
    Would you be so kind as to provide a snippet which uses the capture group? I am not clear on how to proceed based of this (purely oblivious) – AGE Mar 03 '16 at 18:06
  • Sure hang on, I'm getting a link. –  Mar 03 '16 at 18:09
  • 1
    http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression – Wiktor Stribiżew Mar 03 '16 at 18:10