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"
please advice
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"
please advice
Regex should help
var edited = test.replace(/^,|,$/g,'');
^, matches the comma at the start of the string and ,$ matches the comma at the end ..
, with \| so it ends being test.replace(/^\||\|$/g,'')+ after the commas. .replace(/^,+|,+$/g,'')Even easier
var result = test.slice(1,-1);