2

I'm using Prism.js which is syntax highlighter, and it highlights the word matching certain regex.
I want to match any word after the word git, so I tried to use positive lookbehind like this.

(?<=git )\w+

Unfortunately, it seems that positive lookbehind is not supported, so I have to find a equivalent regex expression of it. Is there any way to match any word after the word git without using positive lookbehind?

For example, I want to do this without positive lookbehind.

"git checkout master" -> only "checkout"
"git log --graph" -> only "log"
"anything after the word git matches" -> only "matches"

Also, I cannot use group because I can't tell Prism to choose certain group. It will always highlight the whole match. For example, (?:git )(\w+)will save any word after the word git in the first group, but it matches the word git and the word after git. So it will highlight

"git checkout master" -> "git checkout"
"git log --graph" -> "git log"
"anything after the word git matches" -> "git matches"

and this is not what I want.

CookieHCl
  • 31
  • 6
  • Try `git\s+(?\w+)` or `git\s+\K\w+`. In general, if you can't access a group and have no lookaround features, or `\K` operator, you can't do what you need. – Wiktor Stribiżew Mar 12 '20 at 08:24
  • @WiktorStribiżew It seems that it supports positive/negative lookahead, but not positive/negative lookbehind. Is it impossible to make positive lookbehind equivalent with lookaheads? – CookieHCl Mar 12 '20 at 08:47
  • It is not possible with consuming patterns. `(?<!^)` = `(?!^)`, but `(?<!abc)` != `(?!abc)` – Wiktor Stribiżew Mar 12 '20 at 08:48
  • 1
    So after reading some documentation. Shouldn't there be a `lookbehind: true` option? When set to `TRUE`, the first capturing group in the regex pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind. [This](https://prismjs.com/extending.html) is what I meant. Check the part about `lookbehind`. As I am not sure what prism is exactly I could be completely of here. But I thought it's worth mentioning. – JvdV Mar 12 '20 at 09:51
  • @JvdV I didn't know that there was a lookbehind option! Thank you. It works just like lookbehind. – CookieHCl Mar 12 '20 at 12:05
  • @CookieHCl, Glad that helped. Thanks to your confirmation I felt comfortable enough putting it down as an answer. Feel free to accept it for others to find. – JvdV Mar 12 '20 at 12:22
  • Ok, it is just a capturing group that does the job. It is a tool-specific feature. The question is still off-topic IMHO, "*It's about general computing hardware and software.*". – Wiktor Stribiżew Mar 12 '20 at 12:24
  • @WiktorStribiżew, possibly. Yet, I still found it interesting and worth sharing =) – JvdV Mar 12 '20 at 12:30

1 Answers1

1

As rightfully mentioned in the comments by @WiktorStribiżew "if you can't access a group and have no lookaround features, or \K operator, you can't do what you need." Generally speaking that would be the case, however, without knowing too much about Prism a search throught it's documentation brought me to this, which at the lookbehind option section states:

"'lookbehind' : This option mitigates JavaScript’s lack of lookbehind. When set to true, the first capturing group in the regex pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind."

enter image description here


The above should mean that you could try a pattern like: (\bgit )\w+ when you have set lookbehind: true.

JvdV
  • 70,606
  • 8
  • 39
  • 70