Detailed introduction to regular expression character classes
这次给大家带来正则表达式字符类的详细介绍,使用正则表达式字符类的注意事项有哪些,下面就是实战案例,一起来看一下。
正则表达式字符类加深理解:
一.基本概念:
字符类相对来说是比较简单的,但是也有可能由于理解不够深入或者大意造成一些误区,下面再来做一下介绍。
字符类[]能够匹配包含在中括号中的一系列字符中的任意一个,但是匹配的结果只能够是其中的一个而不是多个,例如:
var str="abcd"; var reg=/[abcd]/; console.log(str.match(reg));
以上正则表达式只能够匹配字符"a"。
二.使用连字符"-":
字符类可以使用连字符"-"来确定一个匹配的范围,当然使用连字符也是有原则的,前后两个字符是有顺序的,如果使用相同的编码,后面的字符码位应大于或等于前面字符的码位,例如:
[0-9]//正确的
[9-0]//错误的
三.字符类中的特殊字符是否需要转义:
大部分在正则中有特殊意义的字符在匹配其本身时需要进行转义,例如"."可以匹配任意字符,"$"用来匹配字符串的结尾,在字符类中就不需要进行转义,代码实例如下:
var str="abc.efcd"; var reg=/c\./g; console.log(str.match(reg));
以上正则中,在点号前面添加了反斜杠进行转义,所以只会匹配字符串"c.",否则也会匹配"cd"。
但是在字符类中绝大多数元字符都不需要进行转义,必须要进行转义的字符只有"\",字符"^"和"-"是否需要转义要看具体的应用场景,代码实例如下:
实例一:
var str="ab$c.efcd"; var reg=/[$.]/g; console.log(str.match(reg));
以上代码可以匹配字符串中的"$"和"."本身。
实例二:
var str="a\\b$c.efcd"; var reg=/[\\$.]/g; console.log(str.match(reg));
以上代码为匹配反斜杠必须要进行转义。
实例三:
var str="ab-c^569"; var reg=/[\^0\-9]/g; console.log(str.match(reg));
在以上代码中"^"和"-"已经不具有特殊意义了,而是表示它们本身,因为使用了转义。
实例四:
var str="ab-c^569"; var reg=/[-a^c0]/g; console.log(str.match(reg));
上面的代码"-"和"^"就没有进行转义,这是因为它们所在点位置使它们失去了特殊意义。
字符类中也可以使用转义字符:
代码实例如下:
var str="abc<|12"; var reg=/[\w]/g; console.log(str.match(reg));
特别注意:\b在字符类外表示单词边界,但是在字符类内[\b]表示退格符。
四.排除型字符类:
[^]是一个排除型字符类,用以匹配不在中括号中的任一字符,当然匹配结果也只能够是一个字符,例如:
[^antzone]
以上正则可以匹配除了字符"a"、"n"、"t"、"z"、"o"、"n"和"e"之外的任意字符。
排除型字符类同样也支持连字符"-",例如:
[^0-9]
可以匹配除了数字之外的其他字符。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed introduction to regular expression character classes. 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











Users may have seen the term wapi when using the Internet, but for some people they definitely don’t know what wapi is. The following is a detailed introduction to help those who don’t know to understand. What is wapi: Answer: wapi is the infrastructure for wireless LAN authentication and confidentiality. This is like functions such as infrared and Bluetooth, which are generally covered near places such as office buildings. Basically they are owned by a small department, so the scope of this function is only a few kilometers. Related introduction to wapi: 1. Wapi is a transmission protocol in wireless LAN. 2. This technology can avoid the problems of narrow-band communication and enable better communication. 3. Only one code is needed to transmit the signal

Pubg, also known as PlayerUnknown's Battlegrounds, is a very classic shooting battle royale game that has attracted a lot of players since its popularity in 2016. After the recent launch of win11 system, many players want to play it on win11. Let's follow the editor to see if win11 can play pubg. Can win11 play pubg? Answer: Win11 can play pubg. 1. At the beginning of win11, because win11 needed to enable tpm, many players were banned from pubg. 2. However, based on player feedback, Blue Hole has solved this problem, and now you can play pubg normally in win11. 3. If you meet a pub

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

i5 is a series of processors owned by Intel. It has various versions of the 11th generation i5, and each generation has different performance. Therefore, whether the i5 processor can install win11 depends on which generation of the processor it is. Let’s follow the editor to learn about it separately. Can i5 processor be installed with win11: Answer: i5 processor can be installed with win11. 1. The eighth-generation and subsequent i51, eighth-generation and subsequent i5 processors can meet Microsoft’s minimum configuration requirements. 2. Therefore, we only need to enter the Microsoft website and download a "Win11 Installation Assistant" 3. After the download is completed, run the installation assistant and follow the prompts to install Win11. 2. i51 before the eighth generation and after the eighth generation

After updating to the latest win11, many users find that the sound of their system has changed slightly, but they don’t know how to adjust it. So today, this site brings you an introduction to the latest win11 sound adjustment method for your computer. It is not difficult to operate. And the choices are diverse, come and download and try them out. How to adjust the sound of the latest computer system Windows 11 1. First, right-click the sound icon in the lower right corner of the desktop and select "Playback Settings". 2. Then enter settings and click "Speaker" in the playback bar. 3. Then click "Properties" on the lower right. 4. Click the "Enhance" option bar in the properties. 5. At this time, if the √ in front of "Disable all sound effects" is checked, cancel it. 6. After that, you can select the sound effects below to set and click

In today's fast life, in order to improve work efficiency, shortcut keys are an essential work requirement. A shortcut key is a key or key combination that provides an alternative way to perform an action normally performed using a mouse. So what are the edge shortcut keys? What are the functions of edge shortcut keys? The editor below has compiled an introduction to edge shortcut keys. Friends who are interested should come and take a look! Ctrl+D: Add the current page to favorites or reading list Ctrl+E: Perform a search query in the address bar Ctrl+F: Find on the page Ctrl+H: Open the history panel Ctrl+G: Open the reading list panel Ctrl +I: Open the favorites list panel (the test does not seem to work) Ctrl+J: Open

Dogecoin is a cryptocurrency created based on Internet memes, with no fixed supply cap, fast transaction times, low transaction fees, and a large meme community. Uses include small transactions, tips, and charitable donations. However, its unlimited supply, market volatility, and status as a joke coin also bring risks and concerns. What is Dogecoin? Dogecoin is a cryptocurrency created based on internet memes and jokes. Origin and History: Dogecoin was created in December 2013 by two software engineers, Billy Markus and Jackson Palmer. Inspired by the then-popular "Doge" meme, a comical photo featuring a Shiba Inu with broken English. Features and Benefits: Unlimited Supply: Unlike other cryptocurrencies such as Bitcoin

Many users have printer drivers installed on their computers but don't know how to find them. Therefore, today I bring you a detailed introduction to the location of the printer driver in the computer. For those who don’t know yet, let’s take a look at where to find the printer driver. When rewriting content without changing the original meaning, you need to The language is rewritten to Chinese, and the original sentence does not need to appear. First, it is recommended to use third-party software to search. 2. Find "Toolbox" in the upper right corner. 3. Find and click "Device Manager" below. Rewritten sentence: 3. Find and click "Device Manager" at the bottom 4. Then open "Print Queue" and find your printer device. This time it is your printer name and model. 5. Right-click the printer device and you can update or uninstall it.
