SoFunction
Updated on 2025-03-03

Detailed explanation of JavaScript RegExp object usage

When you retrieve a text, you can use a pattern to describe what you want to retrieve. RegExp is this pattern.

A simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, substitution, and more.

You can specify where to search in a string, the type of character to search, and so on.

1. Basic usage

<script>
  // Create regular  var reg = /hello/
  // Detect whether a string has text that conforms to regular format  var flag = ("hello world")
  (flag) // true
</script>

2. Modifier

Modifiers are used to perform case-sensitive and global matching

i is case insensitive

g Global Match

<script>
  // By default, it will be case sensitive  var reg = /hello/
  var flag = ("Hello World")
  (flag) // false
  // Add i modifier case insensitive  var reg2 = /hello/i
  var flag2 = ("Hello World")
  (flag2) // true

  var str = "I'm in a bad mood, really bad"
  // Default non-global matching  var reg3 = /bad/
  var newStr = (reg3,"good")
  // Only one bad was replaced  (newStr) // I'm in a good mood, really bad
  // Global Match  var reg4 = /bad/g
  var newStr2 = (reg4,"good")
  // Replace the global bad to good  (newStr2) // I'm in a good mood, really good

  // Use simultaneously  var reg5 = /bad/gi
</script>

3. Metachar

Metacharacters are characters with special meanings:

. Find a single character, except for newlines and line endings. (In layman's terms, it's any character)

  • \d Match numbers 0-9
  • \D Match non-number
  • \s Match any whitespace characters (no space limit)
  • \S Match non-whitespace characters
  • \w Metacharacter is used to find word characters. (Word characters include: a-z, A-Z, 0-9, and underscore)
  • \W Metacharacter is used to find non-word characters.

4. Square brackets

Square brackets are used to find characters in a range

  • [abc] Match a character in brackets (one of a, b, c)
  • [^abc] Match a character that does not exist in brackets (characters other than a, b, and c)
  • [0-9] Find any number from 0 to 9.
  • [a-z] Find any character from lowercase a to lowercase z.
  • [A-Z] Find any character from capital A to capital Z.
  • [A-z] Find any character from uppercase A to lowercase z.

5. Quantitative words

  • \d{6} Match 6 numbers
  • \d{4,6} Match 4 to 6 numbers
  • \d{4,} Match at least 4 numbers
  • ? 0 or 1 More than 1 mismatch
  • + At least 1
  • * 0 or more

.* will match as long as possible characters (greed)

.*? Will match shorter characters as much as possible (greed is forbidden)

\d{4,6} will match as long as possible characters (greed)

\d{4,6}? Will match shorter characters as much as possible (greed is forbidden)

6. Boundary matching

^n matches any string that starts with n.

n$ matches any string with the ending n.

7. Example

Match mobile phone number

var reg = /^1\d{10}$/;

Match QQ number

var reg = /^[1-9]\d{4,10}$/;

Match ID number

var reg = /^[1-9]\d{16}[Xx\d]$/;

Variable name detection (can only consist of letters, numbers, and underscores, and cannot start with numbers, length 6-15)

var reg = /^[A-z_]\w{5,14}$/;

8. Related methods

test(): Detect whether a regular expression can find matching text in the specified string

<script>
  var str = "hi66morning77"
  // Match 3 consecutive numbers  var reg = /\d{3}/
  ((str)) // false
  // Match 7 consecutive lowercase letters  var reg2 = /[a-z]{7}/
  ((str)) // true
</script>

match(): Find a string, return text that meets regular expression rules as an array, if no global match is specified, it is only searched once. Return null if not found

<script>
  var str = "hi66morning77"
  // Match 2 consecutive numbers Non-global match  var reg = /\d{2}/
  ((reg)[0]) // 66
  // Match 2 consecutive numbers Global match (recommended)  var reg2 = /\d{2}/g
  ((reg2)) // ["66", "77"]  
</script>

search(): Returns the index value of the target from the first occurrence of the string (so global matching is ignored)

<script>
  var str = "hi66morning77"
  // Match 2 consecutive numbers Non-global match  var reg = /\d{2}/
  ((reg)) // 2
  // Match one of a, b, m  var reg2 = /[abm]/
  ((reg2))  // 4   
</script>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.