[英]Javascript regular expression to remove characters from the end of a string
I have a couple strings like: 我有几个像这样的字符串:
net_weight_(lbs)
net_height_(inches)
I need a regular expression that will remove the _(x)
from the end of the string. 我需要一个正则表达式,该正则表达式将从字符串的末尾删除
_(x)
。 So in my two examples, I would be left with: 因此,在我的两个示例中,我将得到:
net_weight
net_height
Any help would be greatly appreciated 任何帮助将不胜感激
If you'd prefer not to use regular expressions you can use lastIndexOf('_')
and the get the substring up to that index. 如果您不想使用正则表达式,则可以使用
lastIndexOf('_')
并获取直到该索引的子字符串。
s = s.substr(0, s.lastIndexOf('_'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.