admin管理员组

文章数量:1389754

How do I split a var, which I got from a text input on a "+", "-", "x" and "^"?

JavaScript

function integralinput() {
var a = document.getElementById("input").value;
console.log(a);
var b = a.split("x" + "^" + "+" + "-");
console.log(b);
}

html:

<input id="input" type="text"><label for="function">Funktion</label>
<input type="button" onclick="integralinput()" value="Run">

How do I split a var, which I got from a text input on a "+", "-", "x" and "^"?

JavaScript

function integralinput() {
var a = document.getElementById("input").value;
console.log(a);
var b = a.split("x" + "^" + "+" + "-");
console.log(b);
}

html:

<input id="input" type="text"><label for="function">Funktion</label>
<input type="button" onclick="integralinput()" value="Run">
Share Improve this question asked Jan 31, 2018 at 12:17 user8271158user8271158 2
  • 2 You can split on regexes instead of strings... Have you tried a.split(/[x^+-]/) – jas7457 Commented Jan 31, 2018 at 12:21
  • beside the obvious, what do you want do after splitting with the array? do you need the operators as well? – Nina Scholz Commented Jan 31, 2018 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 8

Use a regex

var b = a.split(/[x^+-]/)

If you want only number you can use like this

function integralinput() {
var a = document.getElementById("input").value;
var b = a.match(/\d+/g).map(Number);
console.log(b);
}
<input value="10+20-3x5^6" id="input" type="text"><label for="function">Funktion</label>
<input type="button" onclick="integralinput()" value="Run">

本文标签: javascriptsplit on plus and minusStack Overflow