javascript 使用jQuery从字符串中删除最后一个逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19041002/
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 string using jQuery
提问by user537137
I am using the Supersized jQuery plugin and need to remove the last comma from the list of images so it works in IE. The Supersized plugin does not work in IE if there is a comma after the last image, this is a known issue.
我正在使用 Supersized jQuery 插件,需要从图像列表中删除最后一个逗号,以便它在 IE 中工作。如果最后一张图片后有逗号,则 Supersized 插件在 IE 中不起作用,这是一个已知问题。
I am using Business Catalyst, so this is not PHP.
我使用的是 Business Catalyst,所以这不是 PHP。
This is how the list of images appear, with a trailing comma:
这是图像列表的显示方式,尾随逗号:
{image : 'melbourne.jpg'},{image : 'tunnel.jpg'},{image : 'building.jpg'},
What would be the best way to do this?
什么是最好的方法来做到这一点?
jQuery(function($){
$.supersized({
slide_interval : 3000,
transition : 1,
transition_speed : 700,
slides : [ // Slideshow Images
{module_webapps,9198,a template="/Layouts/WebApps/slide.tpl"}
]
});
});
And this is what /Layouts/WebApps/slide.tpl looks like. Basically just looping through the slider images...
这就是 /Layouts/WebApps/slide.tpl 的样子。基本上只是循环浏览滑块图像......
{image : '{tag_bg image_value}'},
回答by Karl-André Gagnon
You can use regulare expression on your string like that :
您可以像这样在字符串上使用正则表达式:
var modifiedString = yourString.replace(/,\s*$/, '');
This will remove the last comma IF there is one and will also remove white space.
如果有最后一个逗号,这将删除最后一个逗号,并且还将删除空格。
回答by Ashwin Parmar
Try substring to remove last comma
尝试子字符串删除最后一个逗号
var data = "{image : 'melbourne.jpg'},{image : 'tunnel.jpg'},{image : 'building.jpg'},";
data = data.substr(0, data.length-1);
console.log( data );
回答by Brian
If Business Catalyst doesn't give you the flexibility to do the Django-style {if forloop.last},{endif}
tags, consider changing your
如果 Business Catalyst 不能让您灵活地执行 Django 风格的{if forloop.last},{endif}
标签,请考虑更改您的
]
into
进入
{}]
or
或者
undefined]
so there won't be a trailing comma. Note that your supersized
plugin will need to know how to process these "incorrect" values.
所以不会有尾随逗号。请注意,您的supersized
插件需要知道如何处理这些“不正确”的值。