Patterns and support flags

var expression = / pattern / flags;

Var expression = / pattern/flag;

The matching mode of a regular expression supports the following three flags.

The modifier describe
G (global) Global matches, applying all strings instead of stopping at the first match
I (ignoreCase) Case insensitive, ignoring the case of the pattern and string
M (multiline) Multiple lines match, reach the end of one line of text, continue to look for the next line

There are two creation methods

Literal and constructor definitions

	var expression = /[bc]at/i;// The literal definition
	var expression = new RegExp("[bc]at"."i");// Constructor definition
Copy the code

The RegExp constructor takes two arguments: RegExp(” string pattern to match “,” optional flag string “)

Note: Both arguments passed to the RegExp constructor must be strings

The difference between:

Because the RegExp constructor’s schema argument is a string, characters are double-escaped in some cases.

Literal pattern Constructor pattern

RegExp instance method

test()

** Receives: ** a string argument.

** Returns a Boolean value.

Returns true if the pattern matches the modified argument, false otherwise.

var example = "000-00-0000.";
var pattern = /\d{3}-\d{2}-\d{4}/;
if (pattern.test(example)){
    alert("The pattern was matched.");
}
Copy the code

exec()

Exec () takes an argument, the string to which the pattern is to be applied, and returns an array containing information about the first match; Returns NULL if there is no match. The Array returned is an instance of Array, but contains two additional attributes: index and input.

  • Index indicates the position of the match in the string.
  • Input represents the string to which the regular expression is applied.

match()

The match() method returns the first character that matches an expression in an array-like form

Example:

	var reg = /ab/;
    var str = "abababababab";
   	console.log(str.match(reg));
Copy the code

	var reg = /ab/g;
    var str = "abababababab";
   	console.log(str.match(reg));
Copy the code

The square brackets

  1. […] To find any character in square brackets

Such as:

[ABC], [0-9], [a-z], [a-z], [a-z], [adgk] 2.] [^… Find any character other than square brackets. ^ denotes not. Note that the ^ denotes not only the character inside []

Such as:

[^ ABC], [^ adpk]

  1. (…… |… |… To find out the one specified in the inside parentheses, or | said

Such as:

(abc|bcd|def)

metacharacters

character Equivalence class meaning
. [^\r\n] All characters except carriage returns and newlines
\d [0-9] Numeric characters
\D [^ 0-9] Non-numeric character
\s [\t\n\x0B\f\r] White space characters
\S [^\t\n\x0B\f\r] Non-whitespace character
\w [a-zA-Z_0-9] Word character (alphanumeric underscore)
\W [^a-zA-Z_0-9] Non-word character

locator

character meaning
^ Matches the beginning of the string
$ Matches the end of the string
\b Matches the boundaries of a word
\B In contrast to \b, matches a non-word boundary

Use cases:

quantifiers

character meaning
n+ Matches any string that contains at least one n
n* Matches any string containing zero or more n’s
n? Matches any string containing zero or one n
n{x} Matches a string containing a sequence of x n
n{x,y} Matches a string containing a sequence of x to y n
n{x,} Matches a string containing a sequence of at least x n
? =n Matches any string immediately followed by the specified string n
? ! n Matches any string that is not immediately followed by the specified string n

HTML special characters:

exercises

  1. Convert the-first-name to theFirstName?
        var reg = /-(\w)/g;
        var str = "the-first-name";
       	var newStr = str.replace(reg,function($, $1){
       		return $1.toUpperCase();
       	});
        console.log(newStr);
Copy the code
  1. Deduplicate the array?
	var str = "aaaaaaaaaaabbbbbbbbbcccccccc";
        var reg = /(\w)\1*/g;
        console.log(str.replace(reg,"$1"));
Copy the code
  1. Would you please swap the string aaaaaabbbbbb with the form bbbbbbaaaAAA?
        var reg = /(\w{6})(\w{6})/g;
        var str = "aaaaaabbbbbb";
        console.log(str.replace(reg,"$2 $1"));
Copy the code
  1. Truncate the STR string with **.** from back to front 3 digits?
    var reg = / (? =(\B)(\d{3})+$)/g;
    var str = "1000000000";
// 1, (\d{3})+ matches characters with three digits (starting from left by default) 100000000
// 2, (\d{3})+$matches the last character with three digits 000000000
// 3, (\B)(\d{3})+$matches the last character with three digits, but the left side of each matching character is not a boundary
/ / 4,? =n matches any string immediately following the specified string n (? =(\B)(\d{3})+$)
    console.log(str.replace(reg,"."));
Copy the code
  1. How to get digital characters in a string, and in the form of array output, such as dgfhfgh234bhku259fakhdy678fh output is [234259678];
    var str = "dgfhfgh234bhku259fakhdy678fh";
    var newArr = str.match(/\d+/g).map(function(x){
            return +x;
        })
    console.log(newArr);
    / / ES6 approach

Copy the code
  1. Put the-_@Capitalize the last letter of “?
var str='background-color@good-morning_bad-afternoon@fine_thank';

var reg = /[-_@](\w)/g;
var newStr = str.replace(reg,function($, $1){
    return $1.toUpperCase();
});
console.log(newStr); // backgroundColorGoodMorningBadAfternoonFineThank
Copy the code
  1. What kind of email?

Match integers:/ [-? [1-9]\d*)|(0)/

Matching mobile phone number:/^1[34578]\d{9}$/

  1. Write a function that clears Spaces around the string. (Compatible with all browsers)

    function trim (str) {
        if (str && typeof str === 'string') {
            return str.replace(/^(\s*)|(\s*)$/g.""); }}Copy the code