Javascript 我如何在 jQuery 中使用 preg_match?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6031468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:57:23  来源:igfitidea点击:

How can i use preg_match in jQuery?

javascriptjqueryphpjs

提问by Mary

Can i use preg_match to validate phone number in jQuery? Here is my code which does not work

我可以使用 preg_match 来验证 jQuery 中的电话号码吗?这是我的代码不起作用

if (!preg_match("/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/", phone.val() ))  {
            phone.addClass("needsfilled");
            phone.val(phonerror);
        }

HTML <input id="phone" type="text" value="" name="phone" />

HTML <input id="phone" type="text" value="" name="phone" />

Could any one help plz

任何人都可以帮助plz

回答by Spudley

Javascript includes a regular expression engine, which is accessed via the string.match(), string.replace()and string.split()functions.

Javascript 包含一个正则表达式引擎,可通过string.match(),string.replace()string.split()函数访问。

For example:

例如:

var mystring = "this is a sentence";
mystring = mystring.replace(/sentence/,'string');

var matches = mystring.match(/\b\w+\b/);

That should provide you with essentially the same functionality as preg_match().

这应该为您提供与preg_match().

So to do the same validation as the preg_match in your question:

因此,要进行与问题中的 preg_match 相同的验证:

if(!phone.val().match(/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/)) {
     phone.addClass("needsfilled");
     phone.val(phonerror);
}

If you absolutely insist on having a function called preg_match()which works exactly like the PHP version, there is a project called PHPJS, which aims to implement PHP functions in Javascript. A quick look at their site shows that although they don't currently have a fully working preg_match()implementation, they have an unfinished implementationwhich you may want to look at. But frankly, the built-in Javascript .match()function should be more than sufficient.

如果您绝对坚持调用一个preg_match()与 PHP 版本完全相同的函数,那么有一个名为PHPJS的项目,它旨在在 Javascript 中实现 PHP 函数。快速浏览一下他们的网站会发现,虽然他们目前没有一个完整的preg_match()实现,但他们有一个未完成的实现,你可能想看看。但坦率地说,内置的 Javascript.match()功能应该绰绰有余。

回答by Quentin

To use a PHP function, you will have to make an HTTP request to the server (with whatever data you want to test) and then parse the response.

要使用 PHP 函数,您必须向服务器发出 HTTP 请求(使用您要测试的任何数据),然后解析响应。

Better to use JavaScript's native regular expression functionality.

最好使用JavaScript 的原生正则表达式功能

回答by seoul

RegExp.exec(string)will work.

RegExp.exec(string)将工作。

try this:

尝试这个:

var pattern= "/your pattern/";
var result = pattern.exec ( $("#phone").val() );
// true if match


src: http://javascriptkit.com/javatutors/redev3.shtml

源代码:http: //javascriptkit.com/javatutors/redev3.shtml

回答by Zach Rattner

Instead of using preg_match, which is a PHP function, you should use the String object's matchfunction. See the Mozilla docsfor information on using the match function.

preg_match您应该使用 String 对象的match函数,而不是使用PHP函数。有关使用 match 函数的信息,请参阅Mozilla 文档