1. Escape symbols

First of all, what is an escape symbol

If you want to put quotation marks inside a string, you can’t write “ASD” asD.

At this point you have to use the escape symbol “\” ————> “asd “asD”

2. Match letters

Just a few quick examples

  • \ n line
  • \ r line end
  • \ t TAB
  • A carriage return stands for \r\n

3. Regular expressions

RegExp();

1. First, the basic format looks like this

var reg = / /; So that's the expression var reg = / ABC/for example; var str = "abcd"; reg.test(str); Var reg = // I; var reg = // I; Var reg = / /g; Var reg = / /m; Var reg = /^a/m; Var STR = "abcd\na"; str.match(reg); The output is ["a","a"];Copy the code

There’s another way to say it, but we don’t use it very much

var reg = new RegExp("","");
Copy the code

The first argument is ABC or something, and the second argument is I,g,m

Now that we have objects we have to look at the pointing problem

Such as

var reg1 = /abcd/; var reg2 = new RegExp(reg1); In this case reg1 and reg2 are the same, but not the same if you change the properties of reg1 and reg2 you don't change the properties of reg2 but if you drop the new keyword, they're the same, classic two keys one doorCopy the code

4. The expression

var reg = / [ ] /g; Here [] represents one. Var reg = /[1234567890][1234567890][1234567890]/g; You can also write [0-9], \dCopy the code

Note that the word “^” is written on the outside and the word “[]” on the inside means “not”.

[^a] means everything except a

5. Yuan character

\w === [0-9a-z_] all alphanumeric and underscore (some website names seem to be standard)

Father === [^ W] Father and son complement each other

\d ===[0-9]

\D === [^d]

\s === Whitespace characters (space, TAB, carriage return, line feed, vertical line feed, page feed)

\S === non-whitespace characters

\b === word boundary

var str = “i am handsome and smart”;

The word boundary here is (I use. Instead). I.. am.. handsome.. and.. smart.

It’s easier to understand

\B === non-word boundaries

6. Quantifiers

Let’s say you want to match a 3-digit number

You can

var reg = /\d{3}/g; var str = "123567908"; console.log(str.match(reg)); / / / "343", "465", "435"]Copy the code

Some symbols represent meanings

N + ————>{1,} 1 to infinity

N * ————>{0,} 0 to infinity

Notice the following situation

var reg = /\w*/g; var str = "abc"; str... Output is [" ABC ",""]Copy the code

Since there is still one logical distance left until the last bit is matched, a “” can be matched if the * is 0.

var reg = /\d*/g; var str = "abc"; str... Output is ["","","",""]Copy the code

Notice how greedy matches are in regular expressions

The way to get rid of the greedy match is to add?

For example, if you are reg = /\d{3,5}/g;

I can match five but I can’t match three

I don’t have five to match three

— — — — — — — — — — —

/^ / What does it start with

/ $/ What does it end with

/^ ABC $/ specifies that ABC must be the same.

Example: Check whether a string starts or ends with a number

var reg = /^\d|\d$/g;
var str = "123abc123";
reg.test(str);
Copy the code

Tests whether a string starts and ends with numbers

var reg = /^\d[\s\S]*\d$/g;
var str = "123abc123";
reg.test(str);
Copy the code

7. Backreference

For example, if you are asked to select a string of the form AABB, what do you do

This is where you can use backreferencing

var reg = /(\w)\1(\w)\2/g; Var STR = "aabb"; var STR = "aabb"; var STR = "aabb"; var reg = /(\w)\1(\w)\2/g; console.log(reg.exec(str)); The output would be:Copy the code

It’s clear that \1 is the “A” in here,\2 is the “B”

Exec actually has a lot of knowledge

There is a lastIndex parameter, which is a cursor that exec matches, and we can change it manually

var reg = /ab/g; var str = "abababab"; reg.exec(str); The output only prints one ["ab"]. Further execution will print ab four times, and the fifth time it will print NULL and start the loopCopy the code

So he actually performs a different match cursor each time, with lastIndex on it

8.replace

A very practical method, see the name to know what is the role

Go straight to the problem: replace AABB with BBAA

var reg = /(\w)\1(\w)\2/g; var str = "aabb"; str.replace(reg,"$2$2$1$1"); Note that replace does not have access to the whole world. If there is no g in the reg, just replace one.Copy the code

9. Forward assertion

Forward lookup and forward assertion
var str = “abaaaa”; // We just want a followed by b
var reg = /a(? =b)/g;Copy the code
var reg = /a(? ! b)/g; // a does not follow B

The title

There are a lot of regular expression topics, so I’m not going to summarize them. I’m going to pick one that I think is really interesting

Given a number of 10,000,000,000, you have to write it as 10,000,000,000

Don’t start, one line

var str =”100000000000″;
var reg = /(? =(\B)(\d{3})+$)/g;
// The space in front of the multidigit cannot be the space in the numeric boundary
console.log(str.replace(reg,”,”));