Regular knowledge points
正则表达式,又称规则表达式,计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。
许多程序设计语言都支持利用正则表达式进行字符串操作。
正则的两种写法:
第一种(标准写法):
new RegExp(规则,修饰符);
第二种(简写):
/规则/
d:一个数字
D:一个非数字
w:一个数字、字母、下划线
W:一个非数字、字母、下划线
n:换行
s:空格
S:非空格
b:边界符
.:任意字符除了n
[]:在中括号里任意选择一个字符。
[^]:排除
{}:量词范围
{0,1}/?:最小可以没有最大1个
{0,}/*:最小没有,最大无限
{1,}/+:最小1个,最大无限
{n}:固定的次数
修饰符:i -> 忽略大小写。
转义符:\
正则的方法-text
test:检测是否正则匹配的内容,如果正则成立那么返回一个布尔值。
正则.test(string);
var str = 'RegExp'; var re = new RegExp('p','i'); console.log(re.test(str));
字符串方法-match
match:将匹配到的字符放到一个数组中。
string.match(正则);
当匹配到的字符只有一个的时候,会显示出多个属性
index: 匹配字符的位置
input: 所有内容
而length为1
match如果找不到返回null
var str = 'das37213dsa321hjkh321321'; console.log(str.match(/\d+/g))
字符串的方法-search
search:找到(可以指定也可以正则匹配)匹配的字符位置。
sting.search(字符串||正则);
var str = 'aacabca523c'; console.log(str.search(/\d/));
search如果找不到返回-1
字符串方法-replace
replace:replace
string.replace(要替换谁||可以用正则,替换成什么||函数);
返回值为替换好的字符串。
注意:
如果使用函数,那么一定要有retrun,不然为undefind
一般情况下
函数内的第一个参数是当前匹配的字符。
函数内的第二个参数是当前匹配的索引值。
函数内的第三个参数是当前总的字符串。
函数内的第四个以后参数都为undefined。
var str = '中国共产党,习近平总书记说:“法轮功是邪教!”'; str.replace(/中国共产党|习近平|法轮功|邪教/g,function($0,$1,$2,$3,$4){ console.log($0,$1,$2,$3,$4); });
子项 - ()
从左往右数
如果正则中有子项
从函数的第一个参数之后就是匹配到的子项值,
当参数的个数等于子项个数+1的时候,之后都为正常模式下的参数了(比如:索引,整个字符,undefined..)
var str = '2017/2/17'; var str = str.replace(/(\d+)\D+(\d+)\D+(\d+)/,function($0,$1,$2,$3){ //console.log($1,$2,$3); return $1 + '年' + $2 + '月' + $3 + '日'; }); console.log(str);
子项重定向
var str = 'ssssssssaaaaaabbbbbb'; str.replace(/(\w)\1+/g,function($0,$1){ console.log($1); });
正则的两种写法:
第一种(标准写法):
new RegExp(规则,修饰符);
第二种(简写):
/规则/
d:一个数字
D:一个非数字
w:一个数字、字母、下划线
W:一个非数字、字母、下划线
n:换行
s:空格
S:非空格
b:边界符
.:任意字符除了n
[]:在中括号里任意选择一个字符。
[^]:排除
{}:量词范围
{0,1}/?:最小可以没有最大1个
{0,}/*:最小没有,最大无限
{1,}/+:最小1个,最大无限
{n}:固定的次数
修饰符:i -> 忽略大小写。
转义符:\
正则的方法-text
test:检测是否正则匹配的内容,如果正则成立那么返回一个布尔值。
正则.test(string);
var str = 'RegExp'; var re = new RegExp('p','i'); console.log(re.test(str));
字符串方法-match
match:将匹配到的字符放到一个数组中。
string.match(正则);
当匹配到的字符只有一个的时候,会显示出多个属性
index: 匹配字符的位置
input: 所有内容
而length为1
match如果找不到返回null
var str = 'das37213dsa321hjkh321321'; console.log(str.match(/\d+/g))
字符串的方法-search
search:找到(可以指定也可以正则匹配)匹配的字符位置。
sting.search(字符串||正则);
var str = 'aacabca523c'; console.log(str.search(/\d/));
search如果找不到返回-1
字符串方法-replace
replace:replace
string.replace(要替换谁||可以用正则,替换成什么||函数);
返回值为替换好的字符串。
注意:
如果使用函数,那么一定要有retrun,不然为undefind
一般情况下
函数内的第一个参数是当前匹配的字符。
函数内的第二个参数是当前匹配的索引值。
函数内的第三个参数是当前总的字符串。
函数内的第四个以后参数都为undefined。
var str = '中国共产党,习近平总书记说:“法轮功是邪教!”'; str.replace(/中国共产党|习近平|法轮功|邪教/g,function($0,$1,$2,$3,$4){ console.log($0,$1,$2,$3,$4); });
子项 - ()
从左往右数
如果正则中有子项
从函数的第一个参数之后就是匹配到的子项值,
当参数的个数等于子项个数+1的时候,之后都为正常模式下的参数了(比如:索引,整个字符,undefined..)
var str = '2017/2/17'; var str = str.replace(/(\d+)\D+(\d+)\D+(\d+)/,function($0,$1,$2,$3){ //console.log($1,$2,$3); return $1 + '年' + $2 + '月' + $3 + '日'; }); console.log(str);
子项重定向
var str = 'ssssssssaaaaaabbbbbb'; str.replace(/(\w)\1+/g,function($0,$1){ console.log($1); });
相关推荐:
The above is the detailed content of Regular knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python is currently the most popular programming language. I believe that a large number of novice friends will join the ranks of learning every day. However, no matter how easy a language is to learn, there are still many basic concepts and basic knowledge. For a novice, it is still difficult to master so many in one time. Today, we have collected many Python-related knowledge cheat sheets, which can be said to be all-inclusive. In the future, mom will no longer have to worry about everyone not being able to remember any knowledge points! Python basics Pythonbasics This cheat sheet contains all the basic knowledge of Python, from variable data types to list strings, from environment installation to the use of commonly used libraries, it can be said to be comprehensive. Beginner'sPytho

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

The secret of HTML caching mechanism: essential knowledge points, specific code examples are required In web development, performance has always been an important consideration. The HTML caching mechanism is one of the keys to improving the performance of web pages. This article will reveal the principles and practical skills of the HTML caching mechanism, and provide specific code examples. 1. Principle of HTML caching mechanism During the process of accessing a Web page, the browser requests the server to obtain the HTML page through the HTTP protocol. HTML caching mechanism is to cache HTML pages in the browser

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used
