[英]How can I divide the following equation by regex?
這是我的方程式
5x^2 + 3x - 5 = 50
我用過這個正則表達式
/([+|-])([1-9][a-z])+|([1-9][a-z])+|([+|-])([1-9])+|([1-9])+/mg
但它沒有給出我想要的結果。
我想像這樣划分我的方程
array(
5x^2 ,
3x ,
-5 ,
=50
)
作為起點,您可以通過幾個數學運算符符號(例如+
、 -
、 *
、 /
和=
) 拆分字符串。 然后你會得到一個術語數組,但沒有用於拆分字符串的運算符:
const string = "5x^2 + 3x - 5 = 50"; const regex = /\\+|\\-|\\*|\\/|=/g; const result = string.split(regex); console.info(result);
要檢索定界符,請查看此 StackOverflow 帖子示例。
首先刪除空格。
然后匹配可選=
或-
后跟不是=
或-
或+
示例片段:
var str = "5x^2 + 3x - 5 = 50"; let arr = str .replace(/\\s+/g, '') .match(/[=\\-]*[^+=\\-]+/g); console.log(arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.