In actual work, JavaScript regular expressions are still frequently used. So this part of knowledge is very important. 1. Basic grammar:First: Literal syntax var expression=/pattern/flags; Second: RegExp constructor syntax var pattern = /\w/gi; //literal syntax var pattern = new RegExp('\\w', 'gi'); //constructor syntax, these two are equivalent One thing to note here is that if the regular expression is dynamic, only the second option can be selected. The flags have 3 flags g: indicates a global pattern, that is, the pattern will be applied to all strings instead of stopping immediately when the first match is found; i: indicates a case-insensitive mode, that is, the case of the pattern and the string is ignored when determining the match; m: Indicates multi-line mode, that is, when the end of a line of text is reached, the search continues to see if there is an item matching the pattern in the next line. Of course there are other flags, but they are rarely used and will not be elaborated on in detail. As for what the above \w means, wait a moment, please continue reading. 2. MethodsThe main ones are test(), search(), match(), and replace(). Of course, there are many other methods, which I will not elaborate on here because they are rarely used. 1. Use of test() method Determine whether a string contains a corresponding string 2. Use of search() method Searches for the index of the first occurrence of the string, and returns -1 if not found. 3. Use of match() method Returns an array of matches 4. The use of replace() method is still very common Matches a string and replaces it with another string 3. Matching expressions and actual combat 1. Assertions: An assertion indicates that a match occurs under certain conditions. In short, the concept is a bit confusing, so just read on. Let me continue slowly.
For example I want to match a string that starts with dog and ends with dog, ignoring case. var pattern = /^dog$/i; //Ignore uppercase and lowercase console.log(pattern.test('dog')); //true console.log(pattern.test('sdfdog'));//false console.log(pattern.test('dog56'));//false console.log(pattern.test('dOG'));//true var pattern = /\b\w+/g; //Global match, the + here is a quantifier, representing 1 or more times console.log('Hello World'.match(pattern)); //Output ['Hello','World'], this is the usage of match, returning an array of matches. Here, \b matches the boundary of a word, and \B matches the boundary of a non-word. One lowercase and one uppercase, the uppercase ones are the opposite. Then I don’t need to say more. Let's talk about word boundaries. Maybe many people are not very clear about word boundaries. Let me explain it briefly. For example, the word Hello World has four boundaries, namely H position, o position, W position, and d position. 2. Character class:
3. Scope:
4. Quantifiers:
4. ExpansionMatches a number between 10 and 36 var pattern = /1[2-9]|[2-3][0-9]|4[0-6]/;//12-46 console.log(pattern.test(11));//false console.log(pattern.test(12));//true console.log(pattern.test(20));//true console.log(pattern.test(36));//true console.log(pattern.test(46));//true console.log(pattern.test(47));//false Replace Hello in 'Hello, World! Hello' with Welcome Here we mainly emphasize the use of the replace method in regular expressions, because this is used very frequently in practice. There is a big difference between adding or not adding the g in the flags. var pattern = /Hello/g; var oldString = 'Hello,World!Hello'; var newString = oldString.replace(pattern, 'Welcome'); console.log(newString);//Welcome,World!Welcome SummarizeThis is the end of this article about the practical application of regular expressions in JavaScript. For more relevant JavaScript regular expressions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation on how to avoid the pitfalls of replacing logical SQL in MySQL
>>: Linux debugging tools that developers and operators must look at [Recommended]
The decompressed version of mysql5.7.18 starts th...
Table of contents 1. Ref and reactive 1. reactive...
Which parameter does the rpm command use to insta...
This article records the installation and configu...
This article uses examples to describe how to bac...
A simple calculator written in WeChat applet for ...
1. Add a user . First, use the adduser command to...
Achieve results Implementation Code html <div ...
Free points Interviewer : Have you ever used Linu...
Table of contents 1. ACID Characteristics Transac...
Table of contents 1. innodb_buffer_pool_size 2. i...
Table of contents Vue this.$store.state.xx.xx Get...
The default table name is base_data and the json ...
This article is a self-written imitation of the X...
This article introduces how to create an index on...