这些 javascript 语法叫什么?"/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2, 4})$/"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4640583/
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
What are these javascript syntax called? "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/"
提问by Simon Suh
I have the following code as part of my email validation script. I'd like to learn more about the variable reg
but don't know how to find relevant information because I do not know what the syntax is called. Could someone direct me to the proper resource or tell me the name of this type of syntax?
我将以下代码作为电子邮件验证脚本的一部分。我想了解有关该变量的更多信息,reg
但不知道如何查找相关信息,因为我不知道该语法称为什么。有人可以指导我找到正确的资源或告诉我这种语法的名称吗?
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
回答by Ilya Kogan
It's called a regular expression.
它被称为正则表达式。
There are a lot of resources on regexes, and particularly about regexes in JS. Here is a guide that explains how to use them:
有很多关于正则表达式的资源,特别是关于 JS 中的正则表达式。这是一个解释如何使用它们的指南:
http://www.javascriptkit.com/javatutors/re.shtml
http://www.javascriptkit.com/javatutors/re.shtml
and a guide to the patterns themselves:
以及模式本身的指南:
回答by Sarfraz
Check out the nice wikipedia article:
查看不错的维基百科文章:
In computing, a regular expression, also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.
在计算中,正则表达式,也称为 regex 或 regexp,为匹配文本字符串(例如特定字符、单词或字符模式)提供了一种简洁灵活的方法。正则表达式是用可以由正则表达式处理器解释的形式语言编写的,该程序可用作解析器生成器或检查文本并识别与提供的规范匹配的部分。
回答by Ismail
If you are worry about something, thats nothing to worry about. Thats called Regular Expression AKA Regex. What this script is doing in this code? It is matching/validating the user input to only accept email addresses which are well formatted.
如果您担心某事,那没什么可担心的。这就是所谓的正则表达式又名正则表达式。这个脚本在这段代码中做了什么?它匹配/验证用户输入以仅接受格式良好的电子邮件地址。
test@@@test.com (invalid) [email protected] (valid)
test@@@test.com(无效) [email protected](有效)
回答by Muhoza yves
function check_email() {
函数 check_email() {
// /^[+a-zA-Z0-9]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i this anather regular experession
var pattern = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
if (pattern.test($('#form_email').val()))
{
$('#email_error_message').hide();
}
else
{
$('#email_error_message').html('Invalid Email');
$('#email_error_message').show();
error_email = true;
}
}
}
回答by Starkey
This is a regular expression (regex). Find more information here: http://www.regular-expressions.info/javascript.html.
这是一个正则表达式(regex)。在此处查找更多信息:http: //www.regular-expressions.info/javascript.html。
回答by Chandu
The expression to the right is called a regular expression. You can get more info from : http://www.regular-expressions.info/javascript.html
右边的表达式称为正则表达式。您可以从以下位置获取更多信息:http: //www.regular-expressions.info/javascript.html