1

Can anyone suggest a regEx to change (,,,text,,4,text,3,,) to (text,4,text,3) with javascript String.replace() method?

TBE
  • 1,002
  • 1
  • 11
  • 32
  • 1
    This might be very difficult due to the randomness of the commas... – Brent Dec 02 '15 at 16:51
  • 1
    You don't really need regexes for things like this. Just 1/ Recursively replace all double commas with single ones and 2/ Replace `(,` with `(` and `,)` with `)` – Kerstomaat Dec 02 '15 at 16:52
  • Well that's what i thought, my solution is just remove the parenthesis, perform `String.split(,)` and remove the empty cells from the returned array. – TBE Dec 02 '15 at 16:53

4 Answers4

3

Here is a regex based solution that does it in a single replace call:

var re = /,+(?=,)|,+(?=\))|(\(),+/g; 

var result = input.replace(re, '$1');

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Its leaving some spaces there but not something that we can't handle :) Thanks! – TBE Dec 02 '15 at 17:18
2

I suggest you to make without regular expressions, it's better if you make an array without empty items:

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

var arr = ",,,text,,4,text,3,,".split(","); //convert string into array
arr.clean(""); // clean all empty items

console.log(arr); // returns ["text","4","text","3"]
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • Thanks Marcos, that's pretty much what i had in mind, just wanted be sure as i really like doing stuff with regEx :) – TBE Dec 02 '15 at 16:55
  • Ok, I can recommend you this page https://regex101.com/#javascript to test and improve with explanations your regular expressions, it's a good way to understand and learn to use it – Marcos Pérez Gude Dec 02 '15 at 16:57
  • Mixing in a function to the prototype makes it a bit harder to mantain. Simply `",,,text,,4,text,3,,".split(',').filter(function(s){return !!s;});` would be cleaner. – paolobueno Dec 02 '15 at 17:09
  • Define **cleaner**. I guess that attach a new function to the prototype is more reusable, better maintenance, adds functionality and simplicity. The code you write needs a long explain to understand at the first view. In my answer, you see `array.clean()` and you understand ipso facto what function makes. – Marcos Pérez Gude Dec 02 '15 at 17:13
2

This, most probably, can't be done using a single replace, as Javascript regex is relatively limited, but you could try:

var str = "(,,,text,,4,text,3,,)"
str.replace(/^\(,+/g, "(")
   .replace(/,+\)$/,  ")")
   .replace(/,+/,     ",");
// → "(text,4,text,3)"

Note those are 3 separate cases for the beginning, end (eliminate commas), and middle (replace multiple commas with single ones).

Selfish
  • 6,023
  • 4
  • 44
  • 63
1

You can do it with 3 replaces:

  1. Change all "many commas" to a single comma
  2. Remove the first comma after the first parenthesis.
  3. Remove the last comma before the last parenthesis.

var str = '(,,,text,,4,text,3,,)';

str = str
  .replace(/,+/g, ',')
  .replace(/^\(,/, '(')
  .replace(/,\)$/, ')');

alert(str);
fhelwanger
  • 465
  • 4
  • 9