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

Encountered undefined value during string replacement and weird behaviour #90

Open
Nenrei opened this issue Jul 26, 2023 · 0 comments
Open

Comments

@Nenrei
Copy link

Nenrei commented Jul 26, 2023

I have this string:
let text = "{form 1} {form 2} {form 1:3}";

and this regexp:
/{form (\d+(:\d+)?)}/g
that should match {form x} and {form x:y}

I am executing:

text = reactStringReplace(text, /{form (\d+(:\d+)?)}/g, (match, i) => { return 'match: ${match}, i: ${i}'; });

then in the return of my component I have this:

    return (
        <div className={"cfs-dove__thread-content-message"}>
            {replacedText &&
                replacedText.map((el, i) => {
                    return (
                        <p key={`lettercontent${i}`} dangerouslySetInnerHTML={{ __html: el }}></p>
                    );
                })}
        </div>
    );

and this is de output:

<div class="cfs-dove__thread-content-message">

  <p>match: 1, i: 1</p>

  <p>2</p>

  <p>match: 1:3, i: 7</p>

  <p>:3</p>

  <p>match: , i: 9</p>

</div>

As you can see, the first and third <p> are fine. But the rest aren`t.
The second is the matched value of {form 2} but didn't enter in the replace function.
The forth is only part of the matched value of {form 1:3} and also didn't enter in the function.
And the last did enters in the replace function but the match is "";

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

1 participant