简体   繁体   中英

Remove last comma from string using jQuery

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.

I am using Business Catalyst, so this is not 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...

{image : '{tag_bg image_value}'},

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.

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 );

If Business Catalyst doesn't give you the flexibility to do the Django-style {if forloop.last},{endif} tags, consider changing your

]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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