-
Notifications
You must be signed in to change notification settings - Fork 55
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
Is there any way to run the replace on only the first match? #45
Comments
Hey @jacobedawson, those flags should only be set if you don't supply your own regex, so I don't think that's the issue. I'd guess you're running into the behavior of 'hey there\nthis is a new line'.split(/( )/)
If you have a way to incorporate this into the lib that would be great too! |
Hey @iansinnott thanks man, yeah I dug a bit further and saw that the flags were only set on !isRegExp(match), I've adapted the function in my project as I'm on a deadline but if I have some spare time I'll make a pull request - after a lot of searching your package finally put me on the right track! Thanks for making FOSS :) |
I have now created a PR for this change: #91 |
Thanks for a very cool function! What I'm trying to do is replace a user selected string in a body of text with a React component (that contains the selected string). The issue I'm running into is that react-string-replace is replacing all instances of a selection, rather than just the first match.
Since I'm targeting user-selected text (via the Selection API), I'm using a RegExp constructor, like so:
This works absolutely perfectly if the selected string is unique in the body of text, but if there is another match then I run into a duplicate key error (and also highlight multiple selections when I only want the first).
Since the RegExp constructor doesn't include a 'g' flag, as far as I understand it should only match the first occurrence - but I might be mistaken as to how react-string-replace works! Is it possible to have it run only once, and if so could you please give me a tip on how to achieve this?
Thanks in advance,
Jake
EDIT
Just actually checked the code and realized that the g & i flags are set by default - perhaps I could make a pull request to have the regex flags optional?
The text was updated successfully, but these errors were encountered: