正则表达式长度

时间:2017-10-23 15:55:07

标签: javascript regex

您如何看待使用looooooong正则表达式?这会影响性能吗?

这是我的正则表达式之一:( blockquote搞砸了,所以我编辑了它)

([(]((((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])))([+/*-](((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])))|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))*)|(((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)])([+/*-]((-?((([0-9]+)|([(]-[0-9]+[)]))|(([0-9]*i)|([(]-[0-9]*i[)])))[*/]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)]))[+-]((([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)][+/*-]([(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]|(([0-9]*i)|([(]-[0-9]*i[)]))|(([0-9]+)|([(]-[0-9]+[)]))))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+/*-]((([0-9]*i)|([(]-[0-9]*i[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))|(-?(([0-9]+)|([(]-[0-9]+[)]))[+/*-]((([0-9]+)|([(]-[0-9]+[)]))|[(]((-?(([0-9]+)|([(]-[0-9]+[)]))[+-](([0-9]*i)|([(]-[0-9]*i[)])))|(-?(([0-9]*i)|([(]-[0-9]*i[)]))[+-](([0-9]+)|([(]-[0-9]+[)]))))[)]))))+))[)])

我可以使用RegExp这个长度还是不好的做法?

EDIT 我正在构建复数计算器。这个正则表达式由较短的正则表达式组成。这就是我的做法:

    var regularExpression = new (function() 
{
    this.re = "(([0-9]+)|([(]-[0-9]+[)]))",
    this.im = "(([0-9]*i)|([(]-[0-9]*i[)]))",
    this.complexNumber = "[(]((-?"+ this.re + "[+-]" + this.im + ")|(-?" + this.im + "[+-]" + this.re +"))[)]",
    this.expression = "("+
                                                "(-?("+this.re+"|"+this.im+")[*/]("+this.im+"|"+this.re+"))|"+
                                                "(-?"+this.re+"[+-]"+this.im+"[+-]("+this.im+"|"+this.re+"))|"+
                                                "("+this.complexNumber+"[+/*-]("+this.complexNumber+"|"+this.im+"|"+this.re+"))|"+
                                                "(-?"+this.im+"[+/*-](" + this.im+"|"+this.complexNumber+"))|"+
                                                "(-?"+this.re+"[+/*-](" + this.re+"|"+this.complexNumber+"))"+
                                        ")",
    this.simpleExpression = "([(]("+
                                                        "("+this.expression+"([+/*-]("+this.expression+"|"+this.im+"|"+this.re+"|"+this.complexNumber+"))*)|"+
                                                        "(("+this.im+"|"+this.re+"|"+this.complexNumber+")([+/*-]"+this.expression+")+)"+
                                                    ")[)])"
})();

所以我的regularExpression.simpleExpression甚至更长,因为使用表达式几次。这就是为什么我要问和担心。

simpleExpression正在寻找类似的字符串:(12321-12231i + 1233123 *(12i-231)+ 12323-i)

1 个答案:

答案 0 :(得分:1)

正则表达式非常快,当它们以良好的方式写入时(肯定比基于条件的代码快得多)。

但是,您应该问自己的问题是代码是否可维护和可读 - 将来有人可能不得不重写这部分代码,甚至在一段时间后您可能也不会理解它。

此处的第一点可能会回答您的问题:https://hackernoon.com/few-simple-rules-for-good-coding-my-15-years-experience-96cb29d4acd9