我正在尝试删除以下字符串中逗号的最后一次出现:
52 Tex.641,100 S.W.3d 276,60 Tex.472,65 Tex.723,10 S.W.3d 34,
有关如何通过Javascript执行此操作的任何想法?也许和Regex一起?
答案 0 :(得分:1)
尝试var s = '52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34,';
s = s.substr(0, s.lastIndexOf(','));
console.log(s);
// 52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34
。
{{1}}
答案 1 :(得分:1)
`"52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34,".replace(/,$/, "");`
说明:
,
匹配字符,字面意思
$
断言字符串末尾的位置
document.getElementById('test').innerHTML = "52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34,".replace(/,$/, "");

<span id='test'></span>
&#13;
或者,如果逗号始终是字符串的最后一个字符,则只删除字符串的最后一个字符:
"52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34,".slice(0, -1);
document.getElementById('test').innerHTML = "52 Tex. 641, 100 S.W.3d 276, 60 Tex. 472, 65 Tex. 723, 10 S.W.3d 34,".slice(0, -1);
&#13;
<span id='test'></span>
&#13;
答案 2 :(得分:1)
删除字符串中的最后一个字母:
var hello = "hello"
hello = hello.substring(0, hello.length - 1)
// hello is now 'hell'
或在你的情况下
var comma = "1231, 12313, asdfasdf,"
comma = comma.substring(0, comma.length - 1)
// comma is now '1231, 12313, asdfasdf'