OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Split a array javascript using regular expressions

  • Thread starter Thread starter Jermain Singleton
  • Start date Start date
J

Jermain Singleton

Guest
I am trying to split this array in 2 or 3 parts and stuck. This is what I have so far, if I use one of the splice lines it works but if I use all to test cases then does not. I want to:

  1. grab everything before the operator and combine it into one number - variable
  2. grab everything after the operator and combine it into one number - variable
  3. grab the operator and give it a separate variable

Code:
const re = /^(\+\/\-\*)$/

let store1 = ["7", "1", "+", "9", "6"]
store2 = store1.splice(0, store1.indexOf(re));

// This does not work all at once, only if one line is uncommented
//store2 = store1.splice(0, store1.indexOf('+'));
//store2 = store1.splice(0, store1.indexOf('-'));
//store2 = store1.splice(0, store1.indexOf('*'));
//store2 = store1.splice(0, store1.indexOf('/'));

console.log(store1)
console.log(store2)

I tried what I posted in the code, I have tried turning the array.toStrings() and looping through but cant make sense of it yet.

<p>I am trying to split this array in 2 or 3 parts and stuck. This is what I have so far, if I use one of the splice lines it works but if I use all to test cases then does not. I want to:</p>
<ol>
<li>grab everything before the operator and combine it into one number - variable</li>
<li>grab everything after the operator and combine it into one number - variable</li>
<li>grab the operator and give it a separate variable</li>
</ol>
<pre><code>const re = /^(\+\/\-\*)$/

let store1 = ["7", "1", "+", "9", "6"]
store2 = store1.splice(0, store1.indexOf(re));

// This does not work all at once, only if one line is uncommented
//store2 = store1.splice(0, store1.indexOf('+'));
//store2 = store1.splice(0, store1.indexOf('-'));
//store2 = store1.splice(0, store1.indexOf('*'));
//store2 = store1.splice(0, store1.indexOf('/'));

console.log(store1)
console.log(store2)
</code></pre>
<p>I tried what I posted in the code, I have tried turning the array.toStrings() and looping through but cant make sense of it yet.</p>
 
Top