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 replacements in a string are not working properly #48

Open
pavel-mailpoet opened this issue Jan 28, 2020 · 1 comment
Open

Multiple replacements in a string are not working properly #48

pavel-mailpoet opened this issue Jan 28, 2020 · 1 comment

Comments

@pavel-mailpoet
Copy link

@pavel-mailpoet pavel-mailpoet commented Jan 28, 2020

I am using react-string-replace for translations. I have a string and I want to replace a couple of substitutes.
The code looks like this:

ReactStringReplace(
    '[link]This is my link[/link] available everywhere or [shortcode].',
    /(\[link\].*\[\/link\])|(\[shortcode\])/g,
    (match) => {
      console.log('match', match);
    }
  )

From my understanding, my regular expression should match only the first link and the [shortcode]. But the result in the console looks like this:

match [link]This is my link[/link]
match  available everywhere or 
match [shortcode]

I don't understand why is the middle match there. It doesn't match the regular expression.

@mikelpmc
Copy link

@mikelpmc mikelpmc commented Jan 28, 2020

It seems like the library only accepts one group on the passed regex.
Try it with /(\[link\].*\[\/link\]|\[shortcode\])/g

The output now should be:
match [link]This is my link[/link]
match [shortcode]

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.