improve any/all words regex match
doug-patterson opened this issue · 2 comments
I had anyWordsRegexp
and its companion "any" version exported for 1.59.0 for reuse in contexture-mongo futil-js/contexture-mongo#51 but @daedalus28 noticed that the version of the "all" function over there is better than the one here. Here's the comparison of two generated regexes to see the difference for user input "dis comp"
conexture-mongo version: 'computer distance'.match(/.*(?=.*dis.*)(?=.*comp.*).*/)
returned match:
0: "computer distance"
groups: undefined
index: 0
input: "computer distance"
length: 1
current futil version: 'computer distance'.match(/(?=.*dis)(?=.*comp)/)
returned match:
0: ""
groups: undefined
index: 0
input: "computer distance"
length: 1
the contexture-mongo one is better in that it reports the match as the full matching string (the words in whatever order) while the futil one matches the initial empty string if the tested string has the other stuff after that, so it reports having matched the empty string.
these two will be equivalent for RegExp.test
but if anyone ever wants to use the match for something they'll want the contexture-mongo version
note: the current futil version is also better in another way, in the use of _.words. I'll turn in a best-of-both
PR is in. the improvement is minor in that now you get the input string as a match, but it seems better than saying you matched the null string