수식을 구문 분석하는 함수에서 작업했지만 제대로 작동하지 못했습니다. 항상 작동하지 않는 것 같습니다. 텍스트의 일부분을 필터링하지만 전부는 아닙니다.Javascript : 수식을 구문 분석하는 정규식
parseFormula(e) {
var formula = e.value, value = 0.00, tValue = 0.00, tFormula = '', dObj = {};
if(formula !== undefined && formula !== "") {
dObj._formulaIn = formula;
var f = formula.split(/\s/g);
for(var i = 0; i < f.length; i++) {
tFormula = f[i];
// Replacing PI
tFormula = tFormula.replace(/(pi)/gi,Math.PI);
dObj._1_pi_done = tFormula;
// Replacing Squareroot with placeholder
tFormula = tFormula.replace(/(sqrt)/gi,"__sqrt__");
tFormula = tFormula.replace(/(sqr)/gi,"__sqrt__");
tFormula = tFormula.replace(/(kvrt)/gi,"__sqrt__");
tFormula = tFormula.replace(/(kvr)/gi,"__sqrt__");
dObj._2_sqrt_done = tFormula;
// Removing units that may cause trouble
tFormula = tFormula.replace(/(m2||m3||t2||t3||c2||c3)/gi,"");
dObj._3_units_done = tFormula;
// Removing text
tFormula = tFormula.replace(/\D+[^\*\/\+\-]+[^\,\.]/gi,"");
dObj._4_text_done = tFormula;
// Removing language specific decimals
if(Language.defaultLang === "no_NB") {
tFormula = tFormula.replace(/(\.)/gi,"");
tFormula = tFormula.replace(/(\,)/gi,".");
} else {
tFormula = tFormula.replace(/(\,)/gi,"");
}
dObj._5_lang_done = tFormula;
// Re-applying Squareroot
tFormula = tFormula.replace(/(__sqrt__)/g,"Math.sqrt");
dObj._6_sqrt_done = tFormula;
if(tFormula === "") {
f.splice(i,1);
} else {
f[i] = tFormula;
}
dObj._7_splice_done = tFormula;
console.log(dObj);
}
formula = "";
for(var j = 0; j < f.length; j++) {
formula += f[j];
}
try {
value = eval(formula);
}
catch(err) {}
return value === 0 ? 0 : value.toFixed(4);
} else {
return 0;
}
}
이 기능에 사용 된 RegEx에 대해서는 확실하지 않으므로, 왜 도움을 요청하고 있습니까? 예를 들어 /(pi)/
이 정확한 텍스트 "pi"를 가져 와서 3.141로 바꾸는 올바른 방법인지 확신 할 수 없습니다.
감사 어떤 도움을 (나는 순간 eval
를 사용하고 있지만 단지 개발을 위해 사용되는).
편집 :
내가 구문 분석하려고 수식은 사용자 입력 공식이다. 그/그녀는 다음과 같이 입력 할 것입니다 : 2/0.6 pcs of foo * pi bar + sqrt(4) foobar
. 내가 수학적이지 않은 모든 글자를 떼어 내고 나머지를 계산하기를 원하는 곳에. 상기 화학식 2 (2/0.6) * 3.141 + Math.sqrt(4)
=>12.47
편집하는 것으로 해석 될 의미
-
: 그리드의 필드에 의해 ExtJS에 오브젝트를 통해 전달
colIdx
(INT)column
(Ext.grid.column.Column)- 6,752,(문자열)
grid
(Ext.grid.Panel)originalValue
(문자열)record
(Ext.data.Model)row
CSS (선택기)rowIdx
(INT)store
(Ext.data.Store)value
(문자열)view
(Ext.grid.View)가 제대로 작동하려면 JSFiddle를 얻을 현재없는
e
, 다음과 같은 변수를 포함
입니다.
질문은 무엇인가 유사한 토큰 스트림 것입니까? – mishik
이것은 Ext JS와 어떤 관련이 있습니까? –
명백한 실수가있는 경우 RegExs에서 내가 뭘 잘못하고 있는지 궁금합니다. – rnngau