从字符串js regexp中删除尾随数字 [英] Remove trailing numbers from string js regexp
本文介绍了从字符串js regexp中删除尾随数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用JavaScript RegExp从字符串中删除尾随数字。以下是示例。
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!
推荐答案
您可以使用此正则表达式匹配所有尾随数字。
You could use this regex to match all the trailing numbers.
\d+$
然后用空字符串删除匹配的数字。 \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+$/, "")
这篇关于从字符串js regexp中删除尾随数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文