Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Splitting a string based upon characters in javascript
<html>
<body>
    <input type="text" id="mainbox">
    <button onclick="equals()"> </button>
</body>
</html>
<script src="Calculator.js"></script>
var equation = document.getElementById('mainbox').value
function equals(){
    var equationSplit = equation.split(/\+\-/);
    console.log(equationSplit)
}

Im trying to have the array split based upon + and - signs. I'm currently in the process of just splitting it, but it always returns array length 1.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

1) You should move the code of getting the value of the mainbox input value into the equals function, because you want the value after the button is pressed not after the JS is parsed.

var equation = document.getElementById('mainbox').value

2) You can use regex as /[+-]/g

enter image description here

function equals() {
  var equation = document.getElementById('mainbox').value
  var equationSplit = equation.split(/[+-]/g);
  console.log(equationSplit)
}
<input type="text" id="mainbox">
<button onclick="equals()">split</button>

about 4 years ago · Juan Pablo Isaza Report

0

Your regex splits at the first sequence of '+-' which it probably doesn't find in the string.

Make the regex match either one of the characters by itself by wrapping them in [] and set the global flag to make it match multiple occurrences:

var equationSplit = equation.split(/[\+\-]/g);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!