Search code examples
javascriptstringsplitstring-concatenationstring-operations

How to split string with specific condition in Javascipt


I'm trying to split string with "&&" operator. but in my string there is some data with the same "&&" operator which I don't want to split. so is there any way or logic to ignore that operator for split data.

string -

let str = "countryid == 45 && (payerid==82||payerid==84||payerid==79) && (fobid!=1&&fobid!=2)"

Code -

let breakCond = str.split('&&');

Result -

0: "countryid == 45 "
1: " (payerid==82||payerid==84||payerid==79) "
2: " (fobid!=1"
3: "fobid!=2) "

Expected Result -

0: "countryid == 45 "
1: " (payerid==82||payerid==84||payerid==79) "
2: " (fobid!=1&&fobid!=2) "

Solution

  • It seems you could simply include the white spaces.

    let breakCond = str.split(' && ');
    

    If that doesn't do it than you would have to create your own version of split with its own logic.