Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  162] [ 5]  / answers: 1 / hits: 86754  / 11 Years ago, tue, october 8, 2013, 12:00:00

I have a string like



var str= , this,  is a ,,, test string , , to find regex,,in js.  , ;


in which there are multiple spaces in beginning,middle and end of string with commas. i need this string in



var str=this is a test string to find regex in js.;


i found many regex in forum removing spaces , commas separately but i could not join them to remove both.



Please give explanation of regex syntex to if possible .



Thanks in advance


More From » regex

 Answers
1

You can just replace every space and comma with space then trim those trailing spaces:



var str= , this,  is a ,,, test string , , to find regex,,in js.  , ;
res = str.replace(/[, ]+/g, ).trim();


jsfiddle demo


[#75149] Monday, October 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;