简体   繁体   English

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

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

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. 我想删除字符串末尾可能出现的逗号或分号。

ega,b,c, should be converted to a,b,c ega,b,c应转换为a,b,c

a;b;c; A; B; C; should be converted to 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 请帮忙

Solution

This can be solved simply with regex MDN DOCs and .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. 所以把它们放在一起我们正在寻找一个字符串末尾的逗号或分号。

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

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

You can check where a character occurs in a string using indexOf() and lastIndexOf() . 您可以使用indexOf()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: 在您的情况下,当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;

treffynnon's answer is correct. treffynnon的回答是正确的。 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(/[,;]$/,'');

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. 虽然不是那么优雅。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM