在每次出现算术运算符时拆分一个字符串 [英] Split a string at every occurrence of arithmetic operator

查看:72
本文介绍了在每次出现算术运算符时拆分一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下变数

var arithmetic = "96-66.33+99.3/22*58.2";
var arr;
var operator = "/*+-^";

我想拆分 算术出现索引 运算符变量。

最终结果将是 [96, - 66.33,+ 99.3,/ 22,* 58.2];

V.Roudge提供的最终解决方案

var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/(?=[-+*\/])/);
console.log(arithmetic);

推荐答案

最近我可以通过正则表达式。非常接近,但现在还不是。

Closest I could get you through regexp. Very close but not it exactly yet.

var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/([-+*\/])/g);
console.log(arithmetic);

编辑:知道了。

var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/(?=[-+*\/])/);
console.log(arithmetic);

var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/(?=[-+*\/])/);
console.log(arithmetic);

这篇关于在每次出现算术运算符时拆分一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆