Home > Net >  JavaScript divide the string into several parts according to a certain rule
JavaScript divide the string into several parts according to a certain rule

Time:11-12

I'm writing a code with js and outputting a string to a variable. I have sampled this variable below.

var ResultX = "SefaOODQWHCDCHDAJ=SefaNSADJHDDHQKSefaUQEOFQN==";

I need to separate this string based on what's between two words and add it to an array or list. Like this:

var ResultY = ["SefaOODQWHCDCHDAJ=","SefaNSADJHDDHQK","SefaUQEOFQN=="];

Output:

ResultY[0]=SefaOODQWHCDCHDAJ
ResultY[1]=SefaNSADJHDDHQK
ResultY[2]=SefaUQEOFQN==

The reason I'm doing this is because the string I have contains a few Base64 format images. I need to separate them properly so I can process them later.

Can you help with this? Thank you.

I tried several split and replace methods but failed.

CodePudding user response:

We could try using a string match() approach:

var ResultX = "SefaOODQWHCDCHDAJ=SefaNSADJHDDHQKSefaUQEOFQN==";
var parts = ResultX.match(/Sefa.*?(?=Sefa|$)/g);
console.log(parts);

CodePudding user response:

Something like this:

var ResultY = [s.slice(0,x),s.slice(x,y),s.slice(y)];
  • Related