The usage of expression in javascript

  • 2020-03-30 02:58:15
  • OfStack

Recently, when I was learning javaScript and learning regular expression, I found the knowledge was a little confusing, so I wrote a blog post to make a summary.

define

There are two ways to define a reg exp in javascript:

Var exp1 = new exp(" ABC ");

2) directly put the pattern: var exp2 = / ABC /; // attention. No double quotes, just a string

Special characters

The e visual special character is the same as perl's. Just use it

\ d Digit characters
\w Alphanumeric characters (" word characters ")
\s Whitespace characters (space, TAB, newline, and similar)
\D Characters that are not digits
Non - \ W alphanumeric characters
\ S Non - whitespace characters
A period matches all characters except newlines
Here's an easy way to remember:

D = digit so it's a number

W = word so it's a letter

S is equal to space so it's a space

All capitals are inverted.

The brackets []

Put the pattern in the bracket to indicate that any character is true. (same as Java or Perl)

Such as
 
console.log(/[01]/.test("023424")); // true 

console.log(/[01]/.test("13424")); // true 

console.log(/[01]/.test("23424")); // false 

Parentheses ()

That means that everything in the bracket has to be true

Such as
 
console.log(/[01]/.test("013424")); // true 

console.log(/[01]/.test("13424")); // false 

console.log(/[01]/.test("230424")); // false 

console.log(/[01]/.test("230142401")); // true 

Quantifiers

It's the same as Java. This is a good watch. I've always liked it, little brother

Greedy Reluctant Possessive A fancy X? X?? X? + X, once or not at all X * X *? * X + X, zero or more times X + X +? X++ X, one or more times X {n} X {n}? {n} + X X, exactly n times X {n,} X {n,}? X {n,} + X, at least n times X {n, m} X {n, m}? {n, m} + X X, at least n but not more thanm times

Expression object functions provides

1) test this is easy, just put the string of the test into the test(...) Inside, this function will return true/false for match/unmatch

2) exec, this function returns null if match's string is not found.. If found, it returns an array, which contains the matched strings in order

3) string.replace (expression1, string1) this function replaces the match part of expression with string1. In string1, the parenthesized group in expression can be used

To replace some part of it. For example "co - ol". The replace (/ [/ w] + \ [/ w] + /, "$2 - $1"); //"ol-co" can be used up to $9

4) string.replace (expression, function) this is an enhanced version, and is very powerful, you can define any output you want through function. Specific usage is not listed here, please refer to

Click on the open link

Dynamically generate reg expression
This method works when you want to use something in the reg exp that only the runtime knows about
To generate a reg exp, you simply need to build the reg exp with a string and use an exp constructor. (mentioned at the beginning of the article)

Such as:
 
var name = "dear" 

 " oh, my dear " .replace(new Exp(name), "god"); // oh, my god 

However, if there is a special character in the name, it may be used in the regular expression.
So, in that case, we could put a backslash before every character of the input string like:
 
var name = df[]vxv; 
var expName = name.replace("/[^/w/s]/g","\$&"); 
"my name is df[]vxv".replace(new Exp(name), "Bob"); // my name is Bob 

Related articles: