简体   繁体   English

Javascript正则表达式,用于删除字符串末尾的字符

[英]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('_'));

http://jsfiddle.net/Z7qrW/ http://jsfiddle.net/Z7qrW/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式从中间字符串中提取字符,并带有可选的结束字符 - Javascript regular expression to extract characters from mid string with optional end character 如何从javascript中的字符串末尾删除9个字符 - How to remove 9 characters from the end of the string in javascript 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 使用正则表达式从字符串中删除一组特定字符 - Remove a set of specific characters from a string using Regular Expression 正则表达式从字符串的开头和结尾删除某些字符 - Regex Expression to remove certain characters from string expect at the start and end JavaScript正则表达式替换字符串中的转义特殊字符 - JavaScript Regular Expression to Replace Escape Special Characters from String JavaScript:如何使用正则表达式从字符串中删除空行? - JavaScript: how to use a regular expression to remove blank lines from a string? Javascript从字符串的开头和结尾删除特殊字符 - Javascript remove special characters from beginning and end of string Javascript正则表达式删除字母或空格以外的任何字符 - Javascript Regular Expression to remove any characters other than letters or space Javascript使用正则表达式匹配以字符集中的特定字符开头并以相同字符结尾的字符串 - Javascript match a string which start with a specific char from the set of chars and end with same char using Regular Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM