1. Use of the exec() method

rquickExpr = / [0-9] {1, 2} /;
match = rquickExpr.exec( '1' );
// ["1", index: 0, input: "1", groups: undefined]
Copy the code

The exec() method is used to retrieve matches for regular expressions in the string, returning an array containing the results if a match is found, or null if no match is found

tips1

  • Test (): Tests whether a string matches a pattern, and is also a regular match method, but the test() method only returns whether the specified string is matched, and true if it is. No so return false.
  • Match (): Retrieves a specified value within a string or finds a match for one or more regular expressions.

The difference between match and test is that it can directly check whether a value is contained in a string without using a regular expression. The second difference is that the match function can perform one or more pattern matches and return the match results as an array.