rubyregexrubular

Regex issue with finding words partially


I have a regex that's working for some words but not all:

str.scan(/typeaheadResult\(\{\"Q\":("\w+\s?\w+\s?\w+\s?\w+"),\"R\":\[+("\w+\s?\w+\s?\w+\s?\w+")/)

The string that doesn't seem to be captured is below:

if (typeof typeaheadResult !== "undefined") { typeaheadResult({"Q":"crapshoot","R":[]}) }

My regex doesn't work for the above string and I don't think it's because of the inappropriate word used. Here is the rubular permalink to what I tried: http://rubular.com/r/WOr7xYPePs It has the rest of the sample strings which are important.


Solution

  • The part after R:[ must contain at least 4 \w characters between ".

    If it's intended to be optional, you have to add a ?.

    Update

    Just adding a ? at the end of your regex solves the problem: http://rubular.com/r/HUmtoffTmi