remove starting and ending comma from variable in javascript

后端 未结 4 1571
礼貌的吻别
礼貌的吻别 2021-02-13 04:18

i have the below variable in javascript. i want to remove the starting and ending \"comma\" sign using jquery/javascript

var test=\",1,2,3,4,\" <--- 

Expecte         


        
相关标签:
4条回答
  • 2021-02-13 05:01

    test.substring(1, test.length - 1); should do it for you.

    0 讨论(0)
  • 2021-02-13 05:10

    Regex should help

    var edited = test.replace(/^,|,$/g,'');
    

    ^, matches the comma at the start of the string and ,$ matches the comma at the end ..

    0 讨论(0)
  • 2021-02-13 05:15

    Below code sample will do this

            var str = ",1,2,3,4,5,6,7,8,9,";
            str = str.substring(1,str.lastIndexOf(","));
    
    0 讨论(0)
  • Even easier

    var result = test.slice(1,-1);
    
    0 讨论(0)
提交回复
热议问题