Remove trailing numbers from string js regexp

后端 未结 2 1294
生来不讨喜
生来不讨喜 2020-12-02 01:43

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

Input          output

-------------------------
st         


        
相关标签:
2条回答
  • 2020-12-02 02:19

    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+$/, "")
    
    0 讨论(0)
  • 2020-12-02 02:36

    Use .replace():

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

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

    0 讨论(0)
提交回复
热议问题