Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As long as the input remains consistent, then something along these lines would probably suit your needs. I know you said "retain all brackets" but that would make for an ugly output, so I opted for correcting the parentheses instead. You could easily take out that part. </p> <pre><code>function handleInput(str, input){ var inArray = str.split(' '); inArray.forEach(function(v,i,a){ if(v.indexOf("C"+input) &gt; -1){ if(v.indexOf('(') &gt; -1 || v.indexOf(')') &gt; -1) inArray = fixParen(inArray, input, v, i); else if(i+2 &lt; a.length){ inArray.splice(i,2); inArray = fixNumbers(inArray); } else inArray.splice(i-1,2); console.log(inArray.join(' ')); } }); } function fixParen(inArray, input, elem, index){ var elemArray = elem.split(''); var parenNumber = 0; if(elem.indexOf('(') &gt; -1){ elemArray.forEach(function(v){ if(v == '('){ parenNumber++; } }); for (var i = index+2; parenNumber &gt; 0; i+=2){ var tempElem = inArray[i].split(''); var n = tempElem.indexOf(')'); if(n &gt; -1){ tempElem.splice(n,1); inArray[i] = tempElem.join(''); parenNumber--; } } inArray.splice(index,2); return fixNumbers(inArray); } else{ elemArray.forEach(function(v){ if(v == ')'){ parenNumber++; } }); for (var i = index-2; parenNumber &gt; 0; i-=2){ var tempElem = inArray[i].split(''); var n = tempElem.indexOf('('); if(n &gt; -1){ tempElem.splice(n,1); inArray[i] = tempElem.join(''); parenNumber--; } } inArray.splice(index,2); return fixNumbers(inArray); } } function fixNumbers(inArray){ var count = 1; inArray.forEach(function(v,i,a){ if(v.indexOf('C') &gt; -1){ var tempElem = v.split(''); var n = tempElem.indexOf('C'); tempElem.forEach(function(val,ind,arr){ if(parseInt(val,10)) tempElem.splice(ind,1); }); tempElem.splice(n+1,0,count); inArray[i] = tempElem.join(''); count ++; } }); return inArray; } </code></pre> <p>This is actually a much simpler problem than it seems. First you should note that every important part of the string is evenly separated by a ' '. This lets us use <code>.split(' ')</code> to give us a nice array of all the useful parts. Next, the parentheses can only be on the <code>C</code> elements cutting down the amount of logic required. Then, every <code>(</code> has a corresponding <code>)</code> at some point allowing us to completely ignore testing the length of the string as we can assume that there is an element with the matching parentheses. Finally there are only two cases, it is the last <code>C</code> element so we remove the operator right before, or it is not the last and we remove the one after. Parentheses matching aside, the logic for this is pretty short.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload