-5

I have string

var string =  .row-4 .col-2.grid-unit+.grid-unit+.grid-unit,.row-4 .col-3 .grid-unit .row-4 .grid-unit:nth-of-type(2n+3) .show-grid+.show-grid-reportdiv

and i need to remove all plus sign leaving the plus sign inside the brackets from the string using javascript

Py.
  • 3,499
  • 1
  • 34
  • 53
jerin john
  • 203
  • 1
  • 4
  • 11

2 Answers2

1

I'd go with something along those lines:

var i, splits, string = ".row-4 .col-2.grid-unit+.grid-unit+.grid-unit,.row-4 .col-3 .grid-unit .row-4 .grid-unit:nth-of-type(2n+3) .show-grid+.show-grid-reportdiv";
splits = string.split(/(\([^)]+\))/);
for (i = 0; i< splits.length; i++) {
    if (splits[i].charAt(0) !== "(") {
        splits[i] = splits[i].replace("+"," ");
    }
}
string = splits.join();

Another way around (dunno if it's better performance wise) would be to use the following:

var string = ".row-4 .col-2.grid-unit+.grid-unit+.grid-unit,.row-4 .col-3 .grid-unit .row-4 .grid-unit:nth-of-type(2n+3) .show-grid+.show-grid-reportdiv";
function replacer (match, offset, string) { 
    var posOpen = string.indexOf("(",offset);
    var posClose = string.indexOf(")",offset);
    // we replace it if there are no more closing parenthesis or if there is one that is located after an opening one.
    if (posClose === -1 || (posClose > posOpen && posOpen !== -1)) {
        return " ";
    } else {
        return "+";
    }
};
string.replace(/\+/g, replacer);

EDIT: added bergi suggestion for a quicker check inside the loop.

EDIT2: Second solution

Py.
  • 3,499
  • 1
  • 34
  • 53
  • Yeah, but notice that older IEs do not support capturing groups on `split`. Also, the condition should be simplified to `splits[i].charAt(0) !== "("`. – Bergi Jun 06 '13 at 15:10
  • True for the condition, I'll edit accordingly. As for the browser, I assumed the one he uses support it. This string seems to be a css selector and browsers that don't support capturing group on split don't support `:nth-of-type` as well. – Py. Jun 06 '13 at 16:08
0

Use the following code, and let me know if it works :)

var myString =  ".row-4 .col-2.grid-unit+.grid-unit+.grid-unit,.row-4:nth-of-type(2n+3) .col-3 .grid-unit .row-4 .grid-unit:nth-of-type(2n+3) .show-grid+.show-grid-reportdiv";

var myArray = myString.split(/\(.[\(\)A-Za-z0-9-.+]*\)/);

for(var i = 0; i < myArray.length; i++) { 
  myString = myString.replace(myArray[i], myArray[i].replace(/[+]/g,' ')); 
}
Ammar Hasan
  • 2,436
  • 16
  • 22
  • 1
    Interesting approach (which fails on `a(b+c) b+c`, btw), but [do not use `for in` loops on arrays](http://stackoverflow.com/q/500504/1048572)!!! – Bergi Jun 06 '13 at 15:07
  • ah, I see, thanks man about **for-in** loop :) and about failure, a better regular expression may help. – Ammar Hasan Jun 06 '13 at 15:14
  • I don't think another regexp would help - the approach is flawed. But you can try to prove me wrong :-) – Bergi Jun 06 '13 at 15:16
  • @Bergi : Well I actually don't get that how this fails for **a(b+c) b+c**, I have tested it, it works okay! By the way, as you suggested, I have edited the answer to change **for-in** to **for** loop :) – Ammar Hasan Jun 07 '13 at 05:42
  • Yeah sorry, you'd need to use `"a(b+c)b+c"` to see the failure. I just spotted the design fault, but didn't test my claim :-/ – Bergi Jun 07 '13 at 05:51
  • Ah Yes, my bad, that's a fault when using nested structure with regular expressions, :), thanks man again for helping out. @Bergi – Ammar Hasan Jun 07 '13 at 06:03