0

I need to replace a string that has a random number in it.

This is the string

[Async Chat Thread - #<randomNumber>/INFO]:

Here is what i have tried. the \d was one of my many attempts.

str.replace(/\[Async Chat Thread - #\d\/INFO\]:/gi, "");

I am very new to regex so if someone could please explain how i go about this or what i may be doing wrong.

4
  • \d matches a single digit. Do you want to match more than one digit? Commented Nov 12, 2015 at 3:25
  • yes, it is usually > 10 Commented Nov 12, 2015 at 3:26
  • 1
    Then you need to specify repetition: regular-expressions.info/repeat.html . Commented Nov 12, 2015 at 3:26
  • That was easy, thanks Commented Nov 12, 2015 at 3:28

1 Answer 1

1

\d only matches a single digit. If you want to match one or more digits, you need to specify repetition. For example + denotes the occurrence of the previous group one ore more times:

\d+

OTOH, {x, y} specifies that the previous group has to occur at least x and at most y times:

\d{1,3}

Learn more about repetition.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.