Twenty wins the title by learning javascript Regular Expressions

Before using regular are temporary occasionally look no formal learned.
This encounter to prove safety first 20 questions, very interested and feel the need to learn about the formal system of regular.
Topic is willing:
link: https://www.nowcoder.com/questionTerminal/6f8c901d091949a5837e24bb82a731f2
Source: Cattle-off network

Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. However, "12e", "1a3.14", "1.2.3", "± 5" and "12e + 4.3" not.

See cattle off network users "eat anything today," it gives a very sophisticated regular

//s字符串
function isNumeric(s)
{
    return s.search(/^[+-]?\d*(\.\d*)?$/)==0||s.search(/^[+-]?\d+(\.\d*)?[Ee]{1}[+-]?\d+$/)==0;
}

Although I later found some special test problems, but you may wish to learn about this regularization.

Some of the regular method

Excerpt from https://www.runoob.com/js/js-regexp.html
Search () method is used substring search character string specified, retrieving or regular expression matching substring, and return to the starting position of the substring.

var str = "Visit Runoob!"; 
var n = str.search(/Runoob/i);

replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.

var str = document.getElementById("demo").innerHTML; 
var txt = str.replace(/microsoft/i,"Runoob");

test () method is a regular expression methods.
test () method for detecting whether a character string matching a pattern, if the string includes text matching Returns true, otherwise returns false.
The following examples are used to search for the character string "e":

var patt = /e/;
patt.test("The best things in life are free!");

Modifiers

i performs case-insensitive matching.
g perform a global match (find all matches rather than stopping after the first match).
m perform a multi-line matching.

Regular expression pattern

Extracted from https://www.cnblogs.com/mq0036/p/4836439.html
code is equivalent to the matching
lower IEs [ \ n-], other [ \ n-\ R & lt] matches any character other than a newline
\ d [0-9] matching digits
\ D [^ 0-9] non-numeric character matches
\ s [\ n \ r \ t \ f \ x0B] matches a whitespace character
\ S [^ \ n \ r \ t \ f \ x0B] matches a non-whitespace character
\ w [a-zA-Z0-9_ ] matching alphanumeric and underscore
\ W [^ a-zA- Z0-9_] in addition to matching alphanumeric characters other than an underscore

Quantifier (The following table quantifiers are greedy quantifier single appearance)

Code Description
"*" matches the preceding subexpression zero or more times. For example, zo * matches "z" and "zoo". * Is equivalent to {0}.
"+" Matches the preceding subexpression one or more times. For example, 'zo +' will match "zo" and "zoo", but can not match the "z". + Is equivalent to {1}.
? Matches the preceding subexpression zero or one. For example, "do (es)?" Matches "do" or "does" in the "do". ? Is equivalent to {0,1}.
{n} n is a non-negative integer. Matching the determined n times. For example, 'o {2}' does not match the "Bob" in the 'o', but can match the "food" in the two o.
{n,} n is a non-negative integer. Matching at least n times. For example, 'o {2,}' does not match the "Bob" in the 'o', but it can match all o "foooood" in. 'o {1,}' is equivalent to 'o +'. 'o {0,}' is equivalent to 'o *'.
{n, m} m and n are non-negative integers, where n <= m. Match at least n times and match up to m times. Liu, "o {1,3}" will match "fooooood" in the previous three o. 'o {0,1}' is equivalent to 'o?'. Please note that no spaces between the comma and the two numbers.

After finishing the above, let's look at a few examples

var reg = /^\d+\.\d+$/  //是否带有小数。
//\d+代表一个或者多个数字, 然后是转义符 . , 最后匹配 一个或者多个数字。

var reg = /^[0-9]{8}$/   //是否八位数字组成
var reg= /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;  //电话格式
//这里用到了分支条件 | ,要么匹配前面固定电话,要么匹配后面手机。
var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;   //邮件地址
// \w+ 匹配邮箱前面数字或者字符, [a-zA-Z0-9]{2,10} 匹配hotmail或者gmail**, 后面有点不懂,待更新**

This question is the final look of the regular

//s字符串
function isNumeric(s)
{
    return s.search(/^[+-]?\d*(\.\d*)?$/)==0||s.search(/^[+-]?\d+(\.\d*)?[Ee]{1}[+-]?\d+$/)==0;
}

Match before a search is + 12.231 + 12, 23, +.123 (this is not a problem)
after a search match is -3.34e + 23,5e2, but there are questions + 4.e23 also returns true.

Guess you like

Origin blog.csdn.net/weixin_39285712/article/details/89926985