0

Sample Input :- (;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)

Expected output :- 1A,2,100Baxc02,1000D33,10001HGA

Note :- Length of input is not fixed it varies.

My code so far:

var AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
theArray = AlertExpressionWithUid.split(';');
output="";
for (i = 0; i < theArray.length; i++) {
  theelm = theArray[i];
  output = output + theelm;
}
alert(output);

0

5 Answers 5

2

It looks like you want to match substrings between ;s which don't contain && - why not use a regular expression? Using lookbehind:

const AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
const matches = AlertExpressionWithUid.match(/(?<=;)[^;&]+(?=;)/g);
console.log(matches);

Or, since lookbehind is supported only in newer browsers, without lookbehind, you'll have to use a loop instead:

const AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
const re = /;([^;&]+);/g;
let match;
const matches = [];
while ((match = re.exec(AlertExpressionWithUid)) !== null) {
  matches.push(match[1]);
}
console.log(matches);

Sign up to request clarification or add additional context in comments.

Comments

1

It seems that you need the values with odd index of splited array so use a condition in your for loop as i%2 !== 0. Since, you need

var AlertExpressionWithUid ="(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
var output=[];
var theArray = AlertExpressionWithUid.split(';');
for (i=0;i<theArray.length;i++)
{
  if(i%2 !== 0){
    output.push(theArray[i]);
  }
}
output = output.join(',');
console.log(output) ;

You can also refactor your code to use Array.filter() like this:

var AlertExpressionWithUid ="(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
var theArray = AlertExpressionWithUid.split(';');
var output = theArray.filter((item, index) => index%2 !==0 ).join(',');
console.log(output);

Comments

0

Try this.

var AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
theArray = AlertExpressionWithUid.split(';');
var output='';
for (i = 1; i < theArray.length; i+=2) {
  theelm = theArray[i];
  output = output +','+ theelm;
}
console.log(output.slice(1));

Comments

0

Assuming you cannot have < as a value, try this filter

var AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)",
res = AlertExpressionWithUid.split(";")
       .filter(function(item) { return item.indexOf("<") ==-1 });
res.shift(); // drop the leading "("
console.log(res.join(","))

Comments

0

var AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
    AlertExpressionWithUid = AlertExpressionWithUid.substring(1, AlertExpressionWithUid.length-1);//remove "(" and ")"
    theArray = AlertExpressionWithUid.split(';');
    output="";
    for (i = 0; i < theArray.length; i++) {
      if (i%2!=0)
        output = output + (output != '' ? ',' : '') + theArray[i];
    }
alert(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.