javascript 从字符串中删除最后一个逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17720264/
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
Remove Last Comma from a string
提问by AnaMaria
Using JavaScript, how can I remove the last comma, but only if the comma is the last character or if there is only white space after the comma? This is my code. I got a working fiddle. But it has a bug.
使用 JavaScript,如何删除最后一个逗号,但前提是逗号是最后一个字符或者逗号后只有空格?这是我的代码。我得到了一个工作小提琴。但它有一个错误。
var str = 'This, is a test.';
alert( removeLastComma(str) ); // should remain unchanged
var str = 'This, is a test,';
alert( removeLastComma(str) ); // should remove the last comma
var str = 'This is a test, ';
alert( removeLastComma(str) ); // should remove the last comma
function removeLastComma(strng){
var n=strng.lastIndexOf(",");
var a=strng.substring(0,n)
return a;
}
回答by Jon
This will remove the last comma and any whitespace after it:
这将删除最后一个逗号和它后面的任何空格:
str = str.replace(/,\s*$/, "");
It uses a regular expression:
它使用正则表达式:
The
/
mark the beginning and end of the regular expressionThe
,
matches the commaThe
\s
means whitespace characters (space, tab, etc) and the*
means 0 or moreThe
$
at the end signifies the end of the string
该
/
标记的开始和正则表达式的结束该
,
逗号匹配该
\s
装置的空白字符(空格,制表符,等),和*
装置0以上将
$
在最后表示该字符串的结尾
回答by e-techpulse
you can remove last comma from a string by using slice() method, find the below example:
您可以使用 slice() 方法从字符串中删除最后一个逗号,找到以下示例:
var strVal = $.trim($('.txtValue').val());
var lastChar = strVal.slice(-1);
if (lastChar == ',') {
strVal = strVal.slice(0, -1);
}
Here is an Example
这是一个例子
function myFunction() {
var strVal = $.trim($('.txtValue').text());
var lastChar = strVal.slice(-1);
if (lastChar == ',') { // check last character is string
strVal = strVal.slice(0, -1); // trim last character
$("#demo").text(strVal);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="txtValue">Striing with Commma,</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
回答by Daniel Sloof
function removeLastComma(str) {
return str.replace(/,(\s+)?$/, '');
}
回答by Daniel Sloof
long shot here
远射在这里
var sentence="I got,. commas, here,";
var pattern=/,/g;
var currentIndex;
while (pattern.test(sentence)==true) {
currentIndex=pattern.lastIndex;
}
if(currentIndex==sentence.trim().length)
alert(sentence.substring(0,currentIndex-1));
else
alert(sentence);
回答by Booboo
The greatly upvoted answer removes not only the final comma, but also any spaces that follow. But removing those following spaces was notwhat was part of the original problem. So:
大大赞成的答案不仅删除了最后一个逗号,还删除了后面的任何空格。但是删除后面的空格并不是原始问题的一部分。所以:
let str = 'abc,def,ghi, ';
let str2 = str.replace(/,(?=\s*$)/, '');
alert("'" + str2 + "'");
'abc,def,ghi '
回答by Max Alexander Hanna
First, one should check if the last character is a comma. If it exists, remove it.
首先,应该检查最后一个字符是否是逗号。如果存在,请将其删除。
if (str.indexOf(',', this.length - ','.length) !== -1) {
str = str.substring(0, str.length - 1);
}
NOTEstr.indexOf(',', this.length - ','.length) can be simplified to str.indexOf(',', this.length - 1)
注意str.indexOf(',', this.length - ','.length) 可以简化为 str.indexOf(',', this.length - 1)
回答by Hristo Valkanov
The problem is that you remove the last comma in the string, not the comma if it's the last thing in the string. So you should put an if to check if the last char is ',' and change it if it is.
问题是您删除了字符串中的最后一个逗号,而不是删除了字符串中最后一个逗号的逗号。因此,您应该放置一个 if 来检查最后一个字符是否为 ',' ,如果是则更改它。
EDIT: Is it really that confusing?
编辑:真的那么令人困惑吗?
'This, is a random string'
'这是一个随机字符串'
Your code finds the last comma from the string and stores only 'This, ' because, the last comma is after 'This' not at the end of the string.
您的代码从字符串中找到最后一个逗号并仅存储 'This, ' 因为最后一个逗号在 'This' 之后而不是在字符串的末尾。
回答by Deepu Reghunath
Remove last comma. Working example
删除最后一个逗号。工作示例
function truncateText() {
var str= document.getElementById('input').value;
str = str.replace(/,\s*$/, "");
console.log(str);
}
<input id="input" value="address line one,"/>
<button onclick="truncateText()">Truncate</button>
回答by Trai H?i Phòng
you can remove last comma:
您可以删除最后一个逗号:
var sentence = "I got,. commas, here,";
sentence = sentence.replace(/(.+),$/, '');
console.log(sentence);