0

I've seen such an JS regexp: /hover_cursor\s(.*?);/i

And am trying to find out why apparently this regexp doesn't match this string (it should match): "hover_cursor pointer;"

var string = '"hover_cursor pointer;"';
var notetag = '/hover_cursor\s(.*?);/i'
a = string.match(notetag);
writeln(String(a)); // null

The one part I don't understand in this regexp is this one: (.*?)

Except for this (.*?) everything should match: the string hover_cursor in the beginning: check. \s whitespace: check. Semicolon in the end: check.

So I'm assuming that the problem lies in this (.*?)

What does this even mean? Any non-whitespace character (.) in any quantity (.*) repeated zero or one time?? This question mark seems superflous to me since * allows zero repetitions anyway??

  • 2
    `notetag` is not a `RegExp` instance, it is a string. Remove the single quotes from around it and everything works just fine. – zzzzBov Jul 24 '17 at 22:21
  • As I consider this to be a simple typographical error [I'm voting to close it as off-topic (#2)](/help/on-topic). – zzzzBov Jul 24 '17 at 22:22
  • 1
    It's a non-greedy match. Notice your regex ends with a semicolon. Without the question mark the regex wouldn't stop at the first semicolon it finds, but the last one. Check this answer on SO: https://stackoverflow.com/questions/11898998/how-can-i-write-a-regex-which-matches-non-greedy – Khauri Jul 24 '17 at 22:24
  • @zzzzBov Thank you. First, this is not a typographical error, but a result of me having to do something in a language I do not know (JS). I assumed regexps should be stored as strings, and I assumed wrongly. I didn't think `/hover_cursor\s(.*?);/i` can be a regex literal. Then, the question about the meaning of the `(.*?)` remains. –  Jul 24 '17 at 22:25
  • @gaazkam then please update your question to reflect the actual state of the code, but [be sure to have taken the time to do research](https://meta.stackoverflow.com/q/261592/497418) as asking about what `(.*?)` does can be easily answered by reading the docs on regular expressions. – zzzzBov Jul 24 '17 at 22:35
  • Just use a **regex literal**: `var notetag = /hover_cursor\s(.*?);/i`. And read what `.*?` means [here](https://regex101.com/r/Op7lFE/1). – Wiktor Stribiżew Jul 24 '17 at 22:55

0 Answers0