I am having two strings separated by multiple commas. I need to retain only a single comma between the two strings. for example I have as follows var cptCode = G1077,,,,,,,,,,D3456 so after removing multiple commas, I will have like this : var cptCode = G1077,D3456 The user can enter 2 commas or 5 commas between the two strings. Maximum number of commas between the two strings will be 10. I request the members to let me know how to achieve this.
cptCode = cptCode.replace(/,+/g,',');
/,+/g is a Regular Expression meaning "find a comma (
,) followed by any further commas (
+), globally within the string (
/.../g)." Each match will then be "replaced" with just a single comma (the second parameter).