javascript 使用javascript从字符串末尾删除逗号或分号(如果存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8941613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:05:10  来源:igfitidea点击:

removing a comma or semicolon from the end of a string if present -using javascript

javascriptregex

提问by sathish kumar

I am getting a comma seperated or semicolon seperated string. I want to remove the comma or the semicolon which may occur at the end of the string.

我得到一个逗号分隔或分号分隔的字符串。我想删除可能出现在字符串末尾的逗号或分号。

e.g.a,b,c, should be converted to a,b,c

ega,b,c, 应转换为 a,b,c

a;b;c; should be converted to a;b;c

a;b;c; 应转换为 a;b;c

I want a javascript or regex expression which removes the comma (,) or the semi colon (;) only if it occurs otherwise the string should be left as it is.

我想要一个 javascript 或 regex 表达式,它仅在出现逗号 ( ,) 或分号 ( ;) 时才删除它,否则字符串应保持原样。

please help

请帮忙

回答by Treffynnon

Solution

解决方案

This can be solved simply with regex MDN DOCsand .replace()MDN DOCs:

这可以通过正则表达式MDN DOC.replace()MDN DOC简单解决:

your_string = your_string.replace(/[,;]$/,'');

Here is a jsfiddle that you can run to demo the code: http://jsfiddle.net/5eksE/1/

这是一个 jsfiddle,您可以运行它来演示代码:http: //jsfiddle.net/5eksE/1/

What's happening

发生了什么

/[,;]$/is the regex portion of this script. The slashes can be ignored as they are just regex delimiters.

/[,;]$/是此脚本的正则表达式部分。斜线可以忽略,因为它们只是正则表达式分隔符。

[]is a container for a range of values that you would like to match in this case ,;.

[]是在这种情况下要匹配的一系列值的容器,;

$indicates the end of the string.

$表示字符串的结尾。

So putting it all together we are hunting for the comma or semi-colon that is right at the end of a string.

所以把它们放在一起,我们正在寻找字符串末尾的逗号或分号。

回答by valanto

Regex to handle both comma and semicolon

正则表达式处理逗号和分号

your_string = your_string.replace(/[,;]$/, "");

回答by Michiel van Oosterhout

You can check where a character occurs in a string using indexOf()and lastIndexOf(). In your case, when lastIndexOf()returns an index that is equal to the length of string minus 1, then the index is for the last character in the string:

您可以使用indexOf()和来检查字符在字符串中出现的位置lastIndexOf()。在您的情况下,当lastIndexOf()返回等于字符串长度减 1 的索引时,则索引是字符串中的最后一个字符:

var index = input.lastIndexOf(";");
if(index == input.length - 1) 
{
  input = input.substring(0, input.length - 1);
}

Or, as a one liner:

或者,作为一个班轮:

input = input.lastIndexOf(";") == input.length - 1 ?  input.substring(0, input.length -1 ) : input;

回答by octern

treffynnon's answer is correct. To elaborate a little, it just checks whether your string matches the regular expression ,$(where $ indicates the end of the string), and if so, replaces the comma with an empty string. If you wanted to check for either a comma or a semicolon, you would just change it to

特雷菲农的回答是正确的。详细说明一下,它只是检查您的字符串是否与正则表达式匹配,$(其中 $ 表示字符串的结尾),如果匹配,则用空字符串替换逗号。如果您想检查逗号或分号,只需将其更改为

your_string = your_string.replace(/[,;]$/,'');

回答by avanderw

I normally do

我通常做

value = value.substring(0, value.length-1)

although I ensure that it is there in my previous loop. Sorry missed the second part of your question. You could put in a ternery

虽然我确保它在我之前的循环中存在。抱歉错过了您问题的第二部分。你可以放入三件套

value = value.substring(0, (value[value.length-1] == ',') ? value.length -1 : value.length);

although it is not that elegant.

虽然它不是那么优雅。