Home > Web Front-end > JS Tutorial > body text

Related understanding of regular expressions

jacklove
Release: 2018-05-21 15:33:01
Original
1655 people have browsed it

This article will explain in detail the relevant knowledge of regular expressions.

What are \d, \w, \s, [a-zA-Z0-9], \b,.,*, ,?,x{3},^,$ respectively?

\d: Metacharacter, matches a number, equivalent to [0-9] (matches one from 0 to 9);
\w: Metacharacter, matches letters or numbers or underscores or Chinese characters, equivalent to [0-9a-zA-Z_];
\s: metacharacter, matches any whitespace character;
[a-zA-Z0-9]: [] specifies a range, Matches one of them. The example matches one of a-z/A-Z/0-9, which is equivalent to \w (except Chinese characters);
\b: metacharacter, matches the beginning or end of a word (word boundary):

var a= "hello helloworld";var reg = /\bhello\b/;
a.match(reg);//The result is "hello";

.: metacharacter, matches all characters except newlines;
*: qualifier, repeated 0 or more times;
: qualifier, repeated 1 or more times, at least 1 time;
? : Qualifier, repeated 0 or 1 times;
x{3}: Qualifier, x appears 3 times ({n} repeated n times; {n,m} repeated n-m times (including n,m); { n,} is repeated at least n times; {,m} is repeated at most m times);
: means negation in [] ([abc] matches any character in abc, [abc] matches any character except abc) ;Other times, it can match the beginning of the string;
$: Match the end of the string; (^hello&: Match the string starting with hello and ending with hello)

Write a function trim(str), Remove the blank characters on both sides of the string

function trim(str) {    return str.replace(/^\s+|\s+$/g,'') //匹配开头或结尾的空白字符,替换成'';
 }
Copy after login

Write a function isEmail(str) to determine whether the user input is an email address

function isEmail(str) {    var reg = /^[a-zA-Z\d_]+\@[a-zA-Z\d]+\.[a-zA-Z\d]+$/g;    return reg.test(str);
 }
Copy after login

Write a function isPhoneNum(str) to determine whether the user input is an email address Mobile phone number

function isPhoneNum(str) {    var reg = /^1[3578]\d{9}$/g;    return reg.test(str);
}
Copy after login

Write a function isValidUsername(str) to determine whether the user input is a legal username (length 6-20 characters, can only include letters, numbers, and underscores)

function isValidUsername(str) {    var reg = /^([a-zA-Z\d_]){6,20}$/g;    return reg.test(str);
}
Copy after login

Write a function isValidPassword(str) to determine whether the user enters a legal password (6-20 characters in length, including only uppercase letters, lowercase letters, numbers, and underscores, and at least two types)

function isValidPassword(str) {    if (/^[a-zA-Z0-9_]{6,20}$/g.test(str)) {        if (/^[a-z]{6,20}$/g.test(str) || /^[A-Z]{6,20}$/g.test(str) || /^[0-9]{6,20}$/g.test(str) || /^[_]{6,20}$/g.test(str)) {            return false;
        }else {            return true;
        }
    }else {        return false;
    }
}
Copy after login

Write a regular expression to get all the colors in the following string

var re = /*正则...*/var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee "console.log( subj.match(re) )  // ['#121212', '#AA00ef']
var re = /#[a-f\d]{6}/ig;var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee";console.log( subj.match(re) )
Copy after login

What does the following code output? Why? Rewrite the code so that it outputs [""hunger"", ""world""]

var str = 'hello  "hunger" , hello "world"';var pat =  /".*"/g;
str.match(pat);
//输出[""hunger" , hello "world""];
Copy after login

//Regular expressions are in greedy mode by default and will match as many matches as possible if the conditions are met;
//Rewrite the code

var str = 'hello  "hunger" , hello "world"';
var pat =  /".*?"/g;  //添加?改成非贪婪模式,尽可能少的匹配;
str.match(pat); //[""hunger"", ""world""]
Copy after login

This article is correct Regular expressions have been explained. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

About the usage of this in Javascript

About Math, array, Date Example

HTML5/CSS3 related knowledge explanation

The above is the detailed content of Related understanding of regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template