16

I am trying to remove the trailing numbers from the string using JavaScript RegExp. Here is the example.

Input          output

-------------------------
string33     => string 
string_34    => string_
str_33_ing44 => str_33_ing
string       => string

Hope above example clears what I am looking for!

2
  • 2
    Describe to us, what have you tried? Commented Dec 29, 2014 at 13:41
  • I tried this one /^(.*)(\d+)$/i. but it was missing missing not working Commented Dec 29, 2014 at 13:58

2 Answers 2

40

You could use this regex to match all the trailing numbers.

\d+$

Then remove the matched digits with an empty string. \d+ matches one or more digits. $ asserts that we are at the end of a line.

string.replace(/\d+$/, "")
Sign up to request clarification or add additional context in comments.

Comments

9

Use .replace():

"string".replace(/\d+$/, '')

A simple demo jsfiddle: http://jsfiddle.net/v8xvrze0/

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.