Home  >  Article  >  Web Front-end  >  Regular expressions and RegExp objects in js

Regular expressions and RegExp objects in js

青灯夜游
青灯夜游forward
2018-11-13 10:50:181637browse

This article brings you an introduction to regular expressions and RegExp objects in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. [Recommended related video tutorials: jQuery Tutorial]

1. The meaning of js regular expression matching characters

. Finds a single character, except newlines and line terminators.
\w Find word characters.
\W Find non-word characters.
\d Find numbers.
\D Find non-numeric characters.
\s Find whitespace characters.
\S Find non-whitespace characters.
\b Matches word boundaries.
\B Matches non-word boundaries.
\0 Find NULL characters.
\n Find the newline character.
\f Find the form feed character.
\r Find the carriage return character.
\t Find the tab character.
\v Find the vertical tab character.
\123 Find the character specified by the octal number 123.
\x35 Find the character specified by the hexadecimal number 35.
\u0055 Finds the Unicode character specified by the hexadecimal number 0055.

2. Control of the number and format of matching characters

a Matches any string containing at least one a.
a* Matches any string containing zero or more a's.
a? Matches any string containing zero or one a.
a{2} Matches a string containing a sequence of 2 a's.
a{2,5} Matches a string containing a sequence of 2 to 5 a's.
a{3,} Matches a string containing a sequence of at least 3 a's.
a$ Matches any string ending in a.
^a Matches any string starting with a.
?=a Matches any string immediately followed by the specified string a.
?!a Matches any string that is not immediately followed by the specified string a.

3. Match characters within the specified range

[abc] Find any characters between square brackets.
[^abc] Finds any characters not between square brackets.
[0-9] Find any number from 0 to 9.
[a-z] Finds any character from lowercase a to lowercase z.
[A-Z] Finds any character from uppercase A to uppercase Z.
[A-z] Finds any character from uppercase A to lowercase z.
[abxy] Find any character within the given set.
[^abxy] Find any character outside the given set.
(red|blue|green) Find any specified option.

4. Modifiers

i Perform case-insensitive matching.
g Perform a global match (find all matches instead of stopping after the first match is found).
m Perform multi-line matching.

5. Methods of RegExp object

exec() method is used to retrieve characters Matches regular expressions in strings. Returns an array containing the matching results. If no match is found, the return value is null.

<script type="text/javascript">
        var txt1='Hello World 2019 !';        
        var patt1 = new RegExp(/\d+/g);        
        var result = patt1.exec(txt1);
        document.write("匹配结果: " + result);      
</script>

 

test() method is used to detect whether a string matches a certain pattern. Returns true if the string string contains text that matches the RegExpObject, false otherwise.

<script type="text/javascript">
    var txt1='Hello World 2019 and 2020!';        
    var patt1 = new RegExp("and");        
    var result = patt1.test(txt1);
    document.write("匹配结果: " + result);      
</script>

 

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of Regular expressions and RegExp objects in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete