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
2 Answers
Reset to default 8Use 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
版权声明:本文标题:javascript - split on plus and minus - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744737325a2622410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论