简体   繁体   中英

Why do I get a null variable?

I translating a Java function for JS, logic remains the same but the results are different. In the test of the regular expression , when I do 40kb test but to compile the code I get a null in a line not assigned. ,但是要编译代码,我在未分配的行中得到了null。

How do I give the match correctly in getNumbers (returning an array)?

Log

Error:
TypeError: getNumbers is null

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. It should be

string.match(regex);

and not

regex.match(string);

In your case: var getNumbers = numberPattern.match(val);

numberPattern is regex and val is the string.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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