Javascript:RegExp.compile()的要点是什么? [英] Javascript: what's the point of RegExp.compile()?

查看:153
本文介绍了Javascript:RegExp.compile()的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想从用户那里获取一个regexp,并运行它对几千个输入字符串。在手册中,我发现 RegExp 对象有一个 .compile()方法,用于加速内容这种情况。但为什么我必须传递regexp字符串到它,如果我已经在构造函数中传递它们?也许这个 compile()本身的构造函数?

I've got a situation where I want to get a regexp from the user and run it against a few thousand input strings. In the manual I found that the RegExp object has a .compile() method which is used to speed things up ins such cases. But why do I have to pass the regexp string to it again if I already passed them in the constructor? Perhaps constructor does the compile() itself?

推荐答案

href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/compile =nofollow noreferrer> RegExp()。compile( )方法已弃用。它基本上与构造函数相同,我假设是为什么它被弃用。

The RegExp().compile() method is deprecated. It's basically the same as the constructor, which I assume is why it was deprecated. You should only have to use the constructor nowadays.

换句话说,您以前可以这么做:

In other words, you used to be able to do this:

var regexp = new RegExp("pattern");
regexp.compile("new pattern");

但现在它与简单调用没有什么不同:

But nowadays it is not any different from simply calling:

var regexp = new RegExp("pattern");
regexp = new RegExp("new pattern");

这篇关于Javascript:RegExp.compile()的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆