Home > Net >  How to split string with specific condition in Javascipt
How to split string with specific condition in Javascipt

Time:10-12

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) "

CodePudding user response:

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.

CodePudding user response:

if there are always no spaces in the ones you want to keep. you can just use:

str.split(' && ')
  • Related