-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global flag not respected on regex pattern #72
Comments
#47 fixes this issue. |
@CampbellMG This actually would be pretty heavy a breaking change to the library. Everyone who hasn't been using the global flag would have to update their regexes. Basically, the global-flag is implicit & assumed to be true by the current implementation. Would a good compromise be a separate setting on the parse command that switches on "non-exhaustive" mode? <ParsedText parse={[{pattern: /a/, numberOfMatches: 1, renderText: () => 'z'}]}> aaaa </ParsedText> This library has been a 0 version forever, so we probably want to avoid breaking changes like this one. Alternatively, for your use-case, you could use a capture-group to collect the original text, and "consume" it. <ParsedText parse={[{pattern: /a(.+$)/, renderText: (text, matches) => 'z' + matches[0]}]}> aaaa </ParsedText> (Note: I haven't tested this, just guessing) |
The second solution won't quite work in my app as the regex passed to the component is user-defined which makes it tricky to modify. Your suggestion of a non-exhaustive switch would work perfectly, although in your example it looks likes you are suggesting a I'm still relatively new to open source but would love to put up a PR with this change if you'd like the help. |
Hi - not sure if this is the right thread to ask, but I saw recent activity so I jumped on it, and seems like my issue might be related somehow? I'm currently not getting all matches returned for a regex pattern like the following: |
These is a confusing problem with global flag right now. Since lib is making substrings on each iteration and extracts matched string part by part https://github.com/taskrabbit/react-native-parsed-text/blob/master/src/lib/TextExtraction.js#L39 I assume smth should be noted in the readme, i.e note for updating package if it's fixed in |
@CampbellMG -- that was a slightly different version of the API I was thinking about. -- "non-exhaustive" should be relatively easy to implement. |
@lizzieshipwreck @sh-helen thanks for the concrete examples! I can add those to the unit tests now and see if it's behaving as you expect. |
@sh-helen This is one of the reasons why I made a release branch, and shipped a beta build -- matchIndex behavior doesn't appear to have unit tests (or my changes didn't break or fix them) and since I haven't been a consumer of that functionality, I can only go by the API description and a guess as to what the output should be. Additionally, it looks like in the past, previous maintainers of this library chose not to maintain a Changelog. I could start doing that, but it wouldn't have historical data. |
@fbartho Sure Having changelog would be really great! |
@sh-helen Thanks! -- The more I think about this, the more I don't know what the global flag was expected to do in the original code.
Not sure where this leaves us as to what we want to happen. |
@sh-helen I have a fix here! https://jsfiddle.net/91v5dyhx/1/ Let me know if this does what you would have expected? /cc @CampbellMG |
I've published v0.0.22-beta.3 with this code! (I messed up when publishing beta.2) |
@fbartho yep, it separates words correct, but for some reason it takes whitespace into account as a separate part also, |
@sh-helen Do you have a test case that demonstrates the problem? Happy to see what I can do! |
@fbartho double checked it again with my project, it seems that it works correct now, thanks for your help! |
Works perfectly, thanks @fbartho! |
Oh great! So we don’t need the suggested nonExhaustive flag then? Or: is that still a feature request? |
Thanks for clarifying! Yeah, I figured we’d still need to add nonExhaustive for your original request. @CampbellMG I’ll take a look tomorrow! |
@CampbellMG -- I started implementing something, but I think I confused myself, and actually don't have a good enough example to make sure I actually got what you expected to happen! Would you mind providing a sample of your non-global pattern, and how many matches you would expect to have for a specific sample text? |
Is the example from the original question okay? Essentially, if I have the text |
@CampbellMG -- I think that works! I implemented most of this in this PR: #80 I'll add a test now and see |
@CampbellMG -- Tests added here: https://github.com/taskrabbit/react-native-parsed-text/pull/80/files#diff-457716221106599b3c1239b4af35508dR323-R363 I think it's working! |
That's phenomenal, thank you for that. So by the looks of it, we set a |
Yeah, that’s right. It felt more flexible than just a Boolean, even if you didn’t directly need that behavior. |
If I utilise a regex with the global flag enabled, like this:
<ParsedText parse={[{pattern: /a/g, renderText: () => 'z'}]}> aaaa </ParsedText>
I would expect the output to be:
zzzz
Whereas, I would expect this:
<ParsedText parse={[{pattern: /a/, renderText: () => 'z'}]}> aaaa </ParsedText>
to output:
zaaa
as the global flag is removed and is only matching the first instance.At the moment the second example will output
zzzz
as the parser will continue matching the pattern against any remaining text.While I understand there may be people depending on this behaviour is there any workaround to ensure only the first match is rendered?
The text was updated successfully, but these errors were encountered: