在JavaScript中比较两个字符串时遇到问题

时间:2019-05-28 15:11:03

标签: javascript string

我正在使用localeCompare()比较两个字符串,即使两个字符串看起来相同,也永远不会将条件标记为TRUE。过去,我在使用隐形字符时遇到了一些麻烦;也许这可能是原因。有没有比较两个字符串的更好方法?

<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
  var str1 = "$y=a|b(x-h)|+k$y=a|b(x−h)|+k";
  var test = getFuncIdFunkType(str1);
  alert(test.toString());
}

//Analyzes latex and gets type and id of function 
function getFuncIdFunkType(expr) {
    expr = expr.replace('\\', '');

    var funcType, funcId;
    switch (expr) {
        case ("$x=a$x=a"): //Lines
            funcType = "vertLine";
            funcId = "1";
            break;
        case ("$y=b$y=b"):
            funcType = "horLine";
            funcId = "1";
            break;
        case ("$y=mx+b$y=mx+b"): //Linear
            funcType = "linFunc";
            funcId = "1";
            break;
        case ("$1=frac{x}{a}+frac{y}{b}$1=xa+yb"):
            funcType = "linFunc";
            funcId = "2";
            break;
        case ("$y=ax^2+bx+c$y=ax2+bx+c"): //Quadratic
            funcType = "quadFunc";
            funcId = "1";
            break;
        case ("$y=a(x-h)^2+k$y=a(x−h)2+k"):
            funcType = "quadFunc";
            funcId = "2";
            break;
        case ("$y=a(x-t)(x-s)$y=a(x−t)(x−s)"):
            funcType = "quadFunc";
            funcId = "3";
            break;
        case ("$y=frac{a}{x}$y=ax"): //Rational
            funcType = "ratFunc";
            funcId = "1";
            break;
        case ("$y=frac{a}{x-h}+k$y=ax−h+k"):
            funcType = "ratFunc";
            funcId = "2";
            break;
        case ("$y=frac{ax+b}{cx+d}$y=ax+bcx+d"):
            funcType = "ratFunc";
            funcId = "3";
            break;
        case ("$y=a(c)^{b(x-h)}+k$y=a(c)b(x−h)+k"): //Exponen
            funcType = "expFunc";
            funcId = "1";
            break;
        //Should enter here
        case ("$y=a|b(x-h)|+k$y=a|b(x−h)|+k"): //Absolute
            funcType = "absFunc";
            funcId = "1";
            break;
        case ("$y=asqrt{b(x-h)}+k$y=a√b(x−h)+k"): //Square root
            funcType = "sqrtFunc";
            funcId = "1";
            break;
        case ("$y=alog_c(b(x-h))+k$y=alogc(b(x−h))+k"): //Log
            funcType = "logFunc";
            funcId = "1";
            break;
        case ("$y=aln(b(x-h))+k$y=aln(b(x−h))+k"): //Nat log
            funcType = "natLogFunc";
            funcId = "1";
            break;
    }

    var fIdFtype = [];
    fIdFtype[0] = funcType;
    fIdFtype[1] = funcId;

    return fIdFtype;
}
</script>

</body>
</html>

我不认为这个问题是重复的。 任何建议都有帮助。

0 个答案:

没有答案