Skip to content

Multiple replacements in a string are not working properly #48

Open

Description

@pavel-mailpoet

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.

Activity

  1. mikelpmc commented on Jan 28, 2020

    @mikelpmc

    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]

  2. nahumzs commented on Oct 10, 2020

    @nahumzs

    this totally took my off-balance, shouldn't this be part of the README.md page? :)

    I can do the PR if you feel like this is relevant information for anyone who wants to use the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions