js regular digital

js regex

1. Understand What are regular expressions?

Regular expressions (Regular Expression), also known as regular expressions, which is simply a concept, if we write a string of prior declaration combination of good character and characters to form a "string of rules" for detecting Conform to the rules. Here you may hear some ignorant, do not panic folks, let's look down↓↓↓

2. Application of regular expressions

Take 163 E-mail as an example, like e-mail addresses to determine the length, whether the beginning of the first letter, if no special characters, determine password strength, compliance with specifications, etc., we need to retrieve the regular judge.
Here Insert Picture Description

3. Common regular expression characters
A single character
. 匹配任意字符(他就是一个点)
Range of characters
[0~9] 匹配0~9的单个字符
[a-zA-Z] 匹配单个数字、字母或者下划线字符
[^ ] 匹配除去范围内的字符,就是除去括号内的,其他的都对 
Within short range of characters
\d   等价于  [0-9]  匹配单个的数字字符
\D   等价于 [^0-9]  匹配单个的非数字字符
\w   等价于[a-zA-Z0-9_ ] 匹配单个数字、字母或者下划线字符
\W   等价于[^a-zA-Z0-9_] 匹配非单个数字、字母或者下划线字符
Whitespace characters
\s   匹配任意的空白字符  空格、回车、换行(\n)、tab键
\S   匹配任意的非空白字符

Note: The backslash are shorthand

Anchor Character
^   行首匹配
$   行尾匹配
^ $  必须以同一内容开头或结尾   
Repeating characters
 注:x代表任意的单个字符或者任意的单个元字符
 x+   至少匹配一个x字符
 x?   匹配0个或者1个x字符
 x*   匹配任意个x字符
 x{m,n}  匹配至少m个,至多n个x字符
 x{n}   匹配n个x字符
 (xyz)   凡是通过小括号括起来的多个字符,当做一个字符进行处理
Alternate characters
  |    讲多个字符连起来   类似于或运算
This is what with what, you must be very ignorant of it, stabilize, it will usher in "really fragrant Law"!

Here Insert Picture Description

Create a regular

1. new declaration
2. new statement will be omitted
3. Constant assignment statement

 var box = new RegExp("hello", "gi");//通过new声明
 var box = RegExp("hello", "gi");//省略new声明
 var box = /hello/gi;//直接常量赋值
 //可以传入两个参数:
  修饰符(没有先后顺序)
      i  忽略大小写
      g  全局匹配
      m  换行匹配

Regular Method

test

Format: Regular .test (string);
function: match whether there is a regular string
Return Value: returns true if there
if there is no return false

var str = "how aRe you";
var box = /are/i;//要记住修饰符代表什么
alert(box.test(str));//输出的是true
exec

Format: Regular .exec (string)
Function: match exists in the regular string
Return Value: If there is a return array, the array of content matching stored inside, and if not, return null

var str = "how aRe you";
var box = /are/i;
alert(box.exec(str).length);
//一般这个用法不常用,记住上一个就好

Support of regular characters

math

Format: String search (regular).
Function: match exists in the regular string
Return Value: If there is, return an array, the array stood, the matched substring

var str = "how are Are ARE you";
var box = /arex/ig;
alert(str.match(box)); //输出结果是 are,Are,ARE
search

Format: string .search (regular)
function: string, the regular expression to find the position of the first occurrence of
the return value: If found, return the found index
lookup not, return -1

var str = "how ARE are you";
var box = /are/i;
alert(str.search(box)); //输出结果是 4
replace

Format: string .replace (regular, newStr);
Function: regular found in the string, and he replaced with a new string.
Returns: the replacement string of successful new

var str = "how are Are ARE you";
var box = /are/ig;
var newStr = str.replace(box, "two");
alert(newStr); //输出的是 how two two two you
split

Format: string .split (regular)
function: using regular string into the string
Return Value: End array into substrings thereof.

var str = "how are Are ARE you";
var box = /are/i;
var arr = str.split(box);
alert(arr); //输出的是 how , , , you

Finally to really fragrant moment, let's look at the case to the column

form validation
 if(oValue.length < 6 || oValue.length > 18){
   oUsernameSpan.style.color = 'red';
   oUsernameSpan.innerHTML = "!长度应为6~18个字符";

   //2、判断首字符是否是字母
    }else if(/[^a-zA-Z]/.test(oValue[0])){
    oUsernameSpan.style.color = 'red';
    oUsernameSpan.innerHTML = "!邮件地址必需以英文字母开头";

    //3、判断所有的字符都是数字、字母和下划线组成  
  }else if(/\W/.test(oValue)){
      oUsernameSpan.style.color = 'red';
      oUsernameSpan.innerHTML = "!邮件地址需由字母、数字或下划线组成";
      }else{
      //要是上面所有的都符合条件的话,就输入下面内容
      oUsernameSpan.style.color = 'green';
      oUsernameSpan.innerHTML = '✅恭喜,该邮件地址可注册';
    }

After reading this text columns we all know more or less know how to write, a regular judge grammar in fact not much more practice, you will be able to master!

This time it's about regular first write the follow-up will continue to update, if any inadequacies please a lot of guidance, look forward to your message.

Published an original article · won praise 14 · views 78

Guess you like

Origin blog.csdn.net/weixin_46377975/article/details/104458449