在javascript中从变量中删除开始和结束逗号 [英] remove starting and ending comma from variable in javascript

查看:54
本文介绍了在javascript中从变量中删除开始和结束逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有以下变量。我想使用jquery / javascript删除开头和结尾的逗号符号

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," <--- 

Expected output: var test="1,2,3,4"

请建议

推荐答案

正则表达式应该帮助

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

^,在开头匹配逗号字符串和,$ 匹配最后的逗号..

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

这篇关于在javascript中从变量中删除开始和结束逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆