This is the 15th day of my participation in the More text Challenge. For more details, see more text Challenge

Regular expressions

An overview of the

Regular expression RegExp

Regular expressions are used to match the pattern of character combinations in a string and are used for form validation.

Regular literals: / expression /;

Data type: Reference data type object

var reg = /abc/;
Copy the code

String method

Split (): method that turns a string into an array

Parameter: a string to cut, and can be a regular expression

Return value: array

var str1 = "abxxxxxxxxxxchvxxbcxxxxv";
console.log(str1.split(/x+/));
Copy the code

Match (): Returns an array for string matching

Parameter: string, re

Return value: an array of matched strings

var str2 = "ahabcfabcj";
console.log(str2.match("abc"));
Copy the code

// The argument can be the regular g for global matching
console.log(str2.match(/abc/g));
Copy the code

Search (): Used to match a string lookup and return the index.

Parameter: string, re

Return value: index value, no characters return -1

// Parameter strings
var str3 = "abcbbbbb";
// Return the result of the first match
console.log(str3.search("b"));
// The argument can also be a regular. Search does not have a global match and only returns the result of the first match
console.log(str3.search(/b/));   / / 1
Copy the code

Replace () is used to match string substitutions

First argument: the matched string (re). Second argument: the new string

Return value: the replacement string

var str4 = "www.baidu.com";
// First argument: string
console.log(str4.replace("baidu"."icketang"));
// The first parameter: re
console.log(str4.replace(/baidu/."icketang"));
Copy the code

The regular way

Exec (): used for string matching, returns an array

Parameter: string

Return value: an array of matched strings. There is no global match. Only the result of the first match is printed

var reg = /abc/;
var str = "abcbbbabcbbb";
console.log(reg.exec(str));
Copy the code

Test (): used for string detection

Parameter: string

Return value: Boolean (detects whether the string contains characters matched by the regular expression) True, false

var str2 = "ahjabcfj";
var reg = /abc/;
if(reg.test(str2)) {
	// If it contains ABC output, yes
	console.log("Contains");
}else {
	console.log("没有");
}
Copy the code

Regular term

A regular expression consists of ordinary characters and special characters.

Common characters: letters, digits

Special character: the character have a special meaning () {} [] \ | ^ $? +.

If you want to match a special match, you need to add an escape \

An exact match

Matching only ordinary characters is an exact match.

// An exact match contains only ordinary characters
var str = "abchhhhabcjjbca";
// Output all abCs of the string
// / ABC/Meaning: The matching character must contain a,b, and c; At the same time, the sequence of ABC combinations cannot be changed
console.log(str.match(/abc/g));
Copy the code

Special predefined characters

\t: TAB

\ n: a carriage return

Character set

[] : represents a character set and is used for possible character matching

Simple character set: All possibilities are written directly inside brackets. Brackets can only match one result at a time

var str = "abcncjahckajayck";
// Match the ABC,ahc,ayc strings to
// [bhy] can match b, can match h, can match y
console.log(str.match(/a[bhy]c/g));
Copy the code

Range classes: Write the same data together, using -links such as [0-9] [a-z]

console.log(str.match(/a[a-z]c/g));
Copy the code

Combined classes: different range matches, such as [0-9a-z]

console.log(str.match(/a[a-z0-9]c/g));
Copy the code

Negative classes: [^] that do not contain these possibilities are written after the open brackets

For example, [^0-9] does not match 0-9

// Negative classes do not contain all of the following possibilities
var str3 = "abcnca9cjahckaja7ckaHc";
console.log(str3.match(/a[^0-9]c/g));
Copy the code

The modifier

Regular expressions can be written with modifiers after //

G: indicates global matching. When the first string that meets the condition is matched, all the strings that meet the condition will not be stopped.

str.match(/a[bhy]c/g)
Copy the code

I: js strictly differentiates size. If I is written, it is case insensitive

More than one modifier can be written

str2.match(/a[a-z]c/gi)
Copy the code

quantifiers

Used to handle characters of the same class that are closely connected.

{n}: a hard quantifier indicates that the character appears for n consecutive times

{n,m} soft quantifier, indicating that the character appears at least n times and not more than M times consecutively

{n,} appears at least n times

+: indicates at least one occurrence

? : Appears 0 or 1 times

* : Any time

The border

^:// Is written at the leftmost part of the regular expression, indicating that all characters following the ^ can be matched as the beginning

: matches the end, which is written on the right of the regular expression. Matches with: and is written on the right of the regular expression. Matches with: and is written on the right of the regular expression. Matches with all preceding characters

"abbcdciabcd".match(/bcd$/g)
Copy the code

\b: indicates word boundary, matching characters at the beginning or end of a word

\B: Non-word boundary

For example:

var str = "hello world";
console.log(str.match(/\b[a-z]+/g));
console.log(str.match(/\b[a-z]+\b/g));
// \B non-word boundary, which does not match at the beginning or end of a word
console.log(str.match(/\B[a-z]+\B/g));
Copy the code

Predefined classes

Character set special writing

. : All characters except carriage returns and line feeds

\ W: All words, characters, letters, numbers, _

\W: Non-word characters

\ d: Numbers

\ D: the number

\s: All whitespace characters, such as Spaces, indents

\S: Non-whitespace characters

Or operator

| says the or operator

console.log("bccccac".match(/(a|b)c/g));
Copy the code

Group match

Used to match consecutive characters, with parentheses representing the whole

console.log("abcabc12".match(/(abc){2}/g));
Copy the code

Group backreference

Group backreference: Indicates that the matched string of the regular expression is used again. \ number (used in regular expressions), $number (used outside regular expressions). A code that starts with 1 and is grouped in sequence. 1, 2, 3…

A grouping is a code.

You can use \ encoding again inside the re (each parenthesis corresponds to an encoding from 1,2,3,4…)

console.log("xyzabc1212abc".match(/([a-z]{3})(\d{2})\2\1/g));
Copy the code

You can also use $encoding outside of regular expressions

// change "123*456" to "456*123"
var str = "123 * 456";
// Again use certain writing parentheses,$encoding
var str1 = str.replace(/(\d{3})\*([0-9]{3})/."$2 * $1");
console.log(str1);
Copy the code

The second argument of replace() can also be written as an anonymous function, the match function, to make the operation more flexible

For example:

// replace() the second argument can be an anonymous function, the first argument must be match, and the other arguments must be $encoding
var str2 = str.replace(/(\d{3})\*(\d{3})/.function (match,$1, $2){
	return $2 + "*" + $1;
});
console.log(str2);
Copy the code

Chinese match

U4e00 -\u9fa5

Is a fixed expression that can only be expressed in regular expressions in Chinese.

console.log("Chinese A match".match(/[\u4e00-\u9fa5]+/g));
Copy the code