25

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

3
  • "Please give explanation of regex syntex to if possible" - Have you looked at MDN's regex page or regular-expressions.info? Commented Oct 8, 2013 at 11:07
  • in other string i need multiple space replaced and multiple comma with single comma, like var str=", this is to replace ,, multiple comma and space. ,"; where i need to replace comma from start and end of string , replace multiple spaces with one space , and multiple comma within string to single comma . as var result_str="this is to replace ,multiple comma and space"; Commented Oct 8, 2013 at 11:18
  • @Dashrath Could you put that in your question please? And when should the comma not be completely removed? Commented Oct 8, 2013 at 11:58

4 Answers 4

44

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thx for a good answer, but what's the real difference between str.replace(/[, ]+/g, " ").trim() and str.replace(/[, ]+/g, " ") without a trim() ? Just checked, results are same (or may be i mistaken somewhere?)
@NikolayTalanov Spaces at the beginning and end, if you had ****hello*** it'd end up as *hello*. Using trim() eliminates this. Note using * to mean space here for clarity...
@Lloyd Exactly right :)
5

you can use reg ex for this

/[,\s]+|[,\s]+/g

var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');

RegEx Explained and Demo

1 Comment

You should make use of the replace function on regex101 :)
5

Try something like this:

var new_string = old_string.replace(/[, ]+/g,' ').trim();

The regex is simply [, ]+ if we were to break this down the \s means ANY whitespace character and , is a literal comma. The [] is a character set (think array) and the + means one or more matches.

We throw in a /g on the end so that it does a global search and replace, otherwise it'd just do it for one match only.

3 Comments

this is producing result as new_string="thisisteststringtofindregexinjs";
Oops my bad, fixed it now I think.
It just replaces commas by spaces, resulting in a string with several spaces between the words.
1

You should be able to use

str.replace(/,/g," ");

The 'g' is the key, you may need to use [,]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.