简体   繁体   English

为什么我得到一个空变量?

[英]Why do I get a null variable?

I translating a Java function for JS, logic remains the same but the results are different. 我为JS翻译了Java函数,逻辑保持不变,但结果不同。 In the test of the regular expression , returns match when I do 40kb test but to compile the code I get a null in a line not assigned. 在正则表达式的测试中 ,当我执行40kb测试时返回match ,但是要编译代码,我在未分配的行中得到了null。

How do I give the match correctly in getNumbers (returning an array)? 如何在getNumbers中正确给出匹配项(返回数组)?

Log 日志记录

Error:
TypeError: getNumbers is null

JS JS

function process(val) {
        var isPeso = "(?:k|m|g)b$";
        var normalizado = val.trim().toLowerCase().replace(" ", "");
        var unidade = "";
        var numberPattern = "\d+(\.\d{1,2})?|(Livre)/i";

        var myArray = normalizado.match(isPeso);
        if (myArray != null) {
            unidade = myArray;

            var getNumbers = numberPattern.match(val);
            var i;
            var valores = [];
            for (i = 0; i < getNumbers.length - 1; i++) {
                valores.push((getNumbers[i]))
            }

            var out = "";
            //Caso [X ou superior]
            if (normalizado.indexOf("superior") > -1) {
                return valores[0] + unidade + " ou superior";
            }
            //Caso [X até Y] e [até Y]
            else if (normalizado.indexOf("até") > -1) {
                //Caso [até Y]
                if (valores.length == 1) {
                    out = "até " + valores[0];
                }
                //Caso [X até Y]
                else {
                    out = valores[0] + " até " + valores[1];
                }
            }

            //Caso [X ou Y] ou [X ou Y ou ...]
            else if (normalizado.indexOf("ou") > -1 || normalizado.indexOf("/") > -1) {
                out = valores[0];
                for (i = 1; i < valores.length -1; i++) {
                    out += valores[i];
                }
            }
            //Caso livre
            else if (normalizado.indexOf("*") > -1 || normalizado.indexOf("livre") > -1) {
                out = "Livre";
            }
            //Caso X
            else {
                if (valores.length > 0) {
                    out = valores[0];
                }
            }
            if (out.length == 0 || out == null) {
                return "";
            }
            return out + unidade;
        }
    }

Java 爪哇

public class IntervaloHandler implements LanguageInterface{
    public static final Pattern NUMBER_PATTERN = Pattern.compile("\\d+(\\.\\d{1,2})?|(Livre)", Pattern.CASE_INSENSITIVE);
     public String normalizar(String in) throws LanguageHandler.NormalizarExcception {
            //Trim e LowerCase
            String normalizado = in.trim().toLowerCase(new Locale("pt", "BR"));

            //Identifica a unidade
            String unidade = "";

            Pattern tempoPattern = Pattern.compile("s$");
            Matcher tempoMatcher = tempoPattern.matcher(normalizado);

            Pattern bytesPattern = Pattern.compile("(k|m|g)b$");
            Matcher matcher = bytesPattern.matcher(normalizado);

            //Bytes (PESO)
            if(matcher.find()){
                unidade = matcher.group();
            }

            //Encontra os valores e formata
            ArrayList<String> valores = new ArrayList<>();
            Matcher m = NUMBER_PATTERN.matcher(in);
            while(m.find()){
                valores.add(m.group().replaceAll(" ", ""));
            }
            String out = "";





            //Caso [X ou superior]
            if(normalizado.contains("superior")){
                return String.format("%s%s ou superior", valores.get(0), unidade);
            }
            //Caso [X até Y] e [até Y]
            else if(normalizado.contains("até")){
                //Caso [até Y]
                if(valores.size() == 1){
                    out = String.format("até %s", valores.get(0));
                }
                //Caso [X até Y]
                else{
                    out = String.format("%s até %s", valores.get(0), valores.get(1));
                }
            }
            //Caso [X ou Y] ou [X ou Y ou ...]
            else if(normalizado.contains("ou") || normalizado.contains("/")){
                out = valores.get(0);
                for(int i=1; i < valores.size(); i++){
                    out += String.format("/%s", valores.get(i));
                }
            }
            //Caso livre
            else if(normalizado.contains("*") || normalizado.contains("livre")) {
                out = "Livre";
            }
            //Caso X
            else {
                if(valores.size() > 0)
                    out = valores.get(0);
            }




            //Coloca a unidade no final
            return out.isEmpty() ? "" : String.format("%s%s", out, unidade);
        }
}

Your syntax of using match is wrong in javascript. 您使用match语法在javascript match是错误的。 It should be 它应该是

string.match(regex);

and not 并不是

regex.match(string);

In your case: var getNumbers = numberPattern.match(val); 在您的情况下: var getNumbers = numberPattern.match(val);

numberPattern is regex and val is the string. numberPattern是正则表达式, val是字符串。

Make it 做了

var getNumbers = val.match(numberPattern);

EDIT: Also use 编辑:也使用

var numberPattern = /\d+(\.\d{1,2})?|(Livre)/i;      // without quotes

See DEMO 演示

And I don't see anything wrong in Java. 而且我在Java中没有发现任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM