views:

109

answers:

3

Hi Guys,

I need to setup a function to remove the first character of a string but only if it is a comma (, - Ive found the substr function but this will remove anything reagrdless of what it is.

I thinking some regex might be involved and i had a look but i cannot work it out

My current code is

 text.value = newvalue.substr(1);

Thanks in advance

+5  A: 
text.value = newvalue.replace(/^,/, '');

Untested. Edit: Tested and true. This is just one way to do it, though.

jensgram
what is to test? replace open anchor comma with empty. you got it.
Sky Sanders
perfect, thankyou very much
kwhohasamullet
@Sky Yup, but I like to run through jsbin.com to verify ... and eliminate typos etc. (It's still morning here in Denmark.)
jensgram
A: 
s = (s.length && s[0] == ',') ? s.slice(1) : s;

Or with a regex:

s = s.replace(/^,/, '');
Max Shawabkeh
A: 
var result = (myString[0] == ',') ? myString.substr(1) : myString;
Jimmy Cuadra