Skip to content
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

Multiple matches on same string #35

Open
eylonronen opened this issue Jul 29, 2018 · 2 comments
Open

Multiple matches on same string #35

eylonronen opened this issue Jul 29, 2018 · 2 comments

Comments

@eylonronen
Copy link

Hi there, when using this twice on the same string what is happens is as follows, the first string matches and an array containing the react component returned from the callback fucktion is returned, and than in the second replace, it skips the symbol that was returned from the first run because it is not a string...
I think the solution would be to use flattenDeep instead of flatten, and clarify that callback functions should return an array instead of a component.

@lukasjapan
Copy link

lukasjapan commented Mar 29, 2022

I could overcome this problem with recursion:

function myReplace(string: string | React.ReactNodeArray): React.ReactNodeArray {
  let result: React.ReactNodeArray;
  result = reactStringReplace(string, /123/, (match, i) => (
    <span key={match + i}>{myReplace(match)}</span>
  ));
  result = reactStringReplace(result, /abc/, (match, i) => (
    <span key={match + i}>{myReplace(match)}</span>
  ));
  // ....
  return result;
}

@evanjmg
Copy link

evanjmg commented Mar 3, 2023

yeah this regex thing doesn't work at all :/
(https?://|www.)[\w-.]+.[\w-.]+(/([\S]+)?)?
https://regexr.com/
www.google.com

then I get www for this library. Should be www.google.com

Update: this regex works /(https?://(?:www.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\s]{2,}|www.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\s]{2,}|https?://(?:www.|(?!www))[a-zA-Z0-9]+.[^\s]{2,}|www.[a-zA-Z0-9]+.[^\s]{2,})/g
just test with .split()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants