JavaScript regular expressions

Grammar

Definition

There are two ways to define JavaScript regular expressions. Define a string that matches a string similar to <%XXX%>

1. Constructor

var reg=new RegExp('<%[^%>]+%>','g');

2. Literal

var reg=/<%[^%>]%>/g;

g: global, full-text search, the default search is to stop after the first result

i: ingore case, ignore case, default case sensitive

m: multiple lines, multiple Line search (change the meaning of ^ and $ so that they match the beginning and end of the line respectively on any line, not just the beginning and end of the entire string)


Metacharacters

An important reason why regular expressions are daunting is that there are too many escape characters and there are many combinations, but the metacharacters of regular expressions (in regular expressions Special characters with special meaning in expressions, which can be used to specify its leading characters) are not many

metacharacters: ( [ { \ ^ $ | ) ? * + .

is not Each metacharacter has its own specific meaning. Metacharacters have different meanings in different combinations. Let’s take a look at the classification.

Predefined special characters

Character Meaning

\ t Horizontal tab character

\r Carriage return character

\n Line feed character

\f Form feed character

\cX Control corresponding to X Character (Ctrl+X)

\v Vertical tab character

\0 Null character


character Class

Simple class

In principle, one regular character corresponds to one character. We can enclose them with [], so that the whole [] corresponds to one character. Such as


alert(/ruby/.test("ruby"));//true

##alert( /[abc]/.test("a"));//true

alert(/[abc]/.test("b"));//true

alert(/[abc]/.test("c"));//true

alert("a bat ,a Cat, a fAt bat ,a faT cat".match(/[bcf]at/gi));//bat,Cat,fAt,bat,faT,cat


Negative class

also makes a fuss in the brackets, adding a metacharacter in front to negate it, indicating that the match cannot be the characters in the brackets.

alert(/[^abc]/.test("a"));//false

alert(/[^abc]/.test("b"));//false

alert(/[^abc]/.test("6"));/ /true

##alert(/[^abc]/.test("gg"));//true

Scope class


Still making a fuss inside the square brackets. Sometimes there are too many matching things, and the types are the same, and it is too troublesome to enter them all. We can use this. The characteristic is that a horizontal line is added in the middle.

Composition class

Still make a fuss inside the square brackets. Allows matching different types of single characters using square brackets.

alert(/[a-f]/.test("b"));//true
##alert (/[a-f]/.test("k"));//false

alert(/[a-z]/.test("h"));//true

alert(/[A-Z]/.test("gg"));//false

alert(/[^H-Y]/ .test("G"));//true

alert(/[0-9]/.test("8"));//true

alert(/[^7-9]/.test("6"));//true

alert(/[a- m1-5\n]/.test("a"))//true

alert(/[a-m1-5\n]/.test(" 3"))//true

alert(/[a-m1-5\n]/.test(a))//true

alert(/[a-m1-5\n]/.test("r"))//false


Predefined class

Characters
Equivalent to

Description

. [^\n\r] Any character except line feed and carriage return

\d [0-9] Numeric characters

\D [^0 -9] Non-numeric characters

\s [ \t\n\x0B\f\r] White space characters

\S [^ \t\n\x0B\f\r] Non White space characters

\w [a-zA-Z_0-9] Word characters (all letters)

\W [^a-zA-Z_0-9] Non-word characters


alert(/\d/.test("3"))//true

##alert(/\d/.test("w "))//false

alert(/\D/.test("w"))//true

alert(/ \w/.test("w"))//true

alert(/\w/.test("SI"))//false

alert(/\W/.test(""))//true

alert(/\s/.test(" "))//true

alert(/\S/.test(" "))//false

alert(/\S/.test(" Positive"))//true

alert(/./.test("美"))//true

alert(/ ./.test(" "))//true

alert(/./.test(a))//true


Methods of String objects that support regular expressions

1. search() method;

This method is used to retrieve a specified substring in a string, or to retrieve a string that matches a regular expression.

Basic syntax: stringObject.search(regexp);

@param The parameter regexp can be the string that needs to be retrieved in stringObject, or it can be the RegExp object that needs to be retrieved.

@return(return value) The starting position of the first substring in stringObject that matches the regexp object. If no matching substring is found, -1 is returned;

Note: The search() method does not perform global matching, it will ignore the flag g, and it does not have the lastIndex attribute of the regexp object, and always It searches from the beginning of the string, and always returns the first position matched by stringObject.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<button onclick="myFunction()">点击</button>
<p id="demo"></p>
<script>
function myFunction() {
    var str = "Visit W3cSchool!"; 
    var n = str.search(/3c/i);
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

2. match() method;

This method is used to retrieve a specified value within a string, or find one or more Match of regular expressions. This method is similar to indexOf() or lastIndexOf(); but it returns the specified value, not the position of the string;

Basic syntax:

stringObject.match(searchValue) or stringObject.match(regexp)

@param(parameter)

searchValue needs to retrieve the value of the string;

regexp: the RegExp object that needs to match the pattern;

@return(return value) stores the array of successful matches; it can match the pattern globally. If it matches globally, it returns an array. If no match is found, then it will return null; the returned array has three elements, the first element stores the matching text, and there are two object attributes; the index attribute indicates the matching text The position of the starting character in stringObject; the input attribute declares a reference to the stringObject object;

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<button onclick="myFunction()">点击</button>
<p id="demo"></p>
<script>
function myFunction() {
    var str = "hello world"; 
    var n = str.match("world");
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

3. replace() method:

This method is used to use some characters to replace other characters in a string, or to replace a substring that matches a regular expression;

Basic syntax: stringObject.replace (regexp/substr,replacement);

@param(parameter)

regexp/substr; String or RegExp object that needs to be replaced.

Replacement: The value of a string, the text to be replaced or a function that generates replacement text.

@return(return value) Returns the new string after replacement

Note: The replace() method of stringObject of a string performs a search and replace operation. There are two replacement modes. , can be either a string or a regular matching pattern. If it is a regular matching pattern, then it can be added with the modifier g, which represents global replacement. Otherwise, it will only replace the first matching string;

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<button onclick="myFunction()">点我</button>
<p id="demo">请访问网站</p>
<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var txt = str.replace("网站","php.cn");
    document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

RegExp object method

1. test() method:

This method is used to detect whether a string matches a certain pattern;

Basic syntax: RegExpObject.test(str);

@param(parameter) str is the string that needs to be detected;

@return (return value) If the string str contains text that matches RegExpObject, return true, otherwise return false;

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<script>
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
</script>
</body>
</html>

2. exec() method:

This method is used to retrieve matches of regular expressions in a string.

Basic syntax: RegExpObject.exec(string)

@param (parameter): string [Required] The string to be retrieved.

@return (return value): Returns an array to store the matching results. If no match is found, the return value is null;

Note: The first element of the returned array Is the text that matches the regular expression. This method also returns two attributes. The index attribute declares the position of the first character of the matching text; the input attribute stores the retrieved string string; if this method is not Globally, the returned array is the same as the array returned by the match() method.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<script>
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
</script>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">点我</button> <p id="demo">请访问网站</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("网站","php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
submitReset Code