Use regex to Remove comma only if it is not in brackets - Javascript RegExp

Javascript examples for RegExp:Bracket

Description

Use regex to Remove comma only if it is not in brackets

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w ww .  j  a va  2 s  .  c om
var string = 'filters=fuelType=D,make=[BMW,CITROEN,DACIA],price=(0,100)';
var result = string.replace(/,\s*(?=[^)^\]]*(?:\(|\[|$))/g, '&');
console.log(result);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials