相关文章推荐
谦逊的茴香  ·  【读史】正始十年,司马懿的微醉与梦回-时政频 ...·  1 年前    · 
逃课的硬币  ·  可视化(Visualization)和图形学 ...·  1 年前    · 
爱笑的紫菜汤  ·  全世界都有点意外,俄乌战场突然传来一个好消息 ...·  1 年前    · 
挂过科的板栗  ·  新一代互联网数字经济创新发展论坛暨第一届We ...·  1 年前    · 
千年单身的红薯  ·  小米手表color蓝牙怎么开启-百度经验·  1 年前    · 
小百科  ›  量词- JavaScript | MDN
数词 match mdn web技术
满身肌肉的铁板烧
1 年前
  • Skip to main content
  • Skip to search
  • Skip to select language
MDN Web Docs
  • References
    • Overview / Web Technology

      Web technology reference for developers

    • HTML

      Structure of content on the web

    • CSS

      Code used to describe document style

    • JavaScript

      General-purpose scripting language

    • HTTP

      Protocol for transmitting web resources

    • Web APIs

      Interfaces for building web applications

    • Web Extensions

      Developing extensions for web browsers

    • Web Technology

      Web technology reference for developers

  • Guides
    • Overview / MDN Learning Area

      Learn web development

    • MDN Learning Area

      Learn web development

    • HTML

      Learn to structure web content with HTML

    • CSS

      Learn to style content using CSS

    • JavaScript

      Learn to run scripts in the browser

    • Accessibility

      Learn to make the web accessible to all

  • Plus
    • Overview

      A customized MDN experience

    • AI Help (beta)

      Get real-time assistance and support

    • Updates

      All browser compatibility updates at a glance

    • Documentation

      Learn how to use MDN Plus

    • FAQ

      Frequently asked questions about MDN Plus

  • Blog
  • Play New
  • AI Help Beta
  • Log in
  • Sign up for free
  1. Web 开发技术
  2. JavaScript
  3. JavaScript 指南
  4. 正则表达式
  5. 量词

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

In this article

  • 尝试一下
  • 类型
  • 举例说明
  • 参见
  • JavaScript
  • JavaScript 基础知识
  • JavaScript 第一步
  • 创建 JavaScript 代码块
  • 介绍 JavaScript 对象
    1. 概述
    2. AggregateError
    3. Array
    4. ArrayBuffer
    5. AsyncFunction
    6. AsyncGenerator
    7. AsyncGeneratorFunction
    8. AsyncIterator
    9. Atomics
    10. BigInt
    11. BigInt64Array
    12. BigUint64Array
    13. Boolean
    14. DataView
    15. Date
    16. decodeURI()
    17. decodeURIComponent()
    18. encodeURI()
    19. encodeURIComponent()
    20. Error
    21. escape()
    22. eval()
    23. EvalError
    24. FinalizationRegistry
    25. Float32Array
    26. Float64Array
    27. Function
    28. Generator
    29. GeneratorFunction
    30. globalThis
    31. Infinity
    32. Int16Array
    33. Int32Array
    34. Int8Array
    35. InternalError
    36. Intl
    37. isFinite()
    38. isNaN()
    39. Iterator
    40. JSON
    41. Map
    42. Math
    43. NaN
    44. Number
    45. Object
    46. parseFloat()
    47. parseInt()
    48. Promise
    49. Proxy
    50. RangeError
    51. ReferenceError
    52. Reflect
    53. RegExp
    54. Set
    55. SharedArrayBuffer
    56. String
    57. Symbol
    58. SyntaxError
    59. TypedArray
    60. TypeError
    61. Uint16Array
    62. Uint32Array
    63. Uint8Array
    64. Uint8ClampedArray
    65. undefined
    66. unescape()
    67. URIError
    68. WeakMap
    69. WeakRef
    70. WeakSet
    表达式和运算符
    1. 概述
    2. Addition (+)
    3. Addition assignment (+=)
    4. Assignment (=)
    5. async function expression
    6. async function* expression
    7. await
    8. Bitwise AND (&)
    9. Bitwise AND assignment (&=)
    10. Bitwise NOT (~)
    11. Bitwise OR (|)
    12. Bitwise OR assignment (|=)
    13. Bitwise XOR (^)
    14. Bitwise XOR assignment (^=)
    15. class expression
    16. Comma operator (,)
    17. Conditional (ternary) operator
    18. Decrement (--)
    19. delete operator
    20. Destructuring assignment
    21. Division (/)
    22. Division assignment (/=)
    23. Equality (==)
    24. Exponentiation (**)
    25. Exponentiation assignment (**=)
    26. Function expression
    27. function* expression
    28. Greater than (>)
    29. Greater than or equal (>=)
    30. Grouping operator ( )
    31. import.meta
    32. import()
    33. in operator
    34. Increment (++)
    35. Inequality (!=)
    36. instanceof
    37. Left shift (<<)
    38. Left shift assignment (<<=)
    39. Less than (<)
    40. Less than or equal (<=)
    41. Logical AND (&&)
    42. Logical AND assignment (&&=)
    43. Logical NOT (!)
    44. Logical OR (||)
    45. Logical OR assignment (||=)
    46. Multiplication (*)
    47. Multiplication assignment (*=)
    48. new operator
    49. new.target
    50. null
    51. Nullish coalescing assignment (??=)
    52. Nullish coalescing operator (??)
    53. Object initializer
    54. Operator precedence
    55. Optional chaining (?.)
    56. Property accessors
    57. Remainder (%)
    58. Remainder assignment (%=)
    59. Right shift (>>)
    60. Right shift assignment (>>=)
    61. Spread syntax (...)
    62. Strict equality (===)
    63. Strict inequality (!==)
    64. Subtraction (-)
    65. Subtraction assignment (-=)
    66. super
    67. this
    68. typeof
    69. Unary negation (-)
    70. Unary plus (+)
    71. Unsigned right shift (>>>)
    72. Unsigned right shift assignment (>>>=)
    73. void operator
    74. yield
    75. yield*
    语句和声明
    1. 概述
    2. async function
    3. async function*
    4. block
    5. break
    6. class
    7. const
    8. continue
    9. debugger
    10. do...while
    11. empty
    12. export
    13. Expression statement
    14. for
    15. for await...of
    16. for...in
    17. for...of
    18. function declaration
    19. function*
    20. if...else
    21. import
    22. label
    23. let
    24. return
    25. switch
    26. throw
    27. try...catch
    28. var
    29. while
    30. with 正则表达式
      1. 概述
      2. Backreference: \1, \2
      3. Capturing group: (...)
      4. Character class escape: \d, \D, \w, \W, \s, \S
      5. Character class: [...], [^...]
      6. Character escape: \n, \u{...}
      7. Disjunction: |
      8. Input boundary assertion: ^, $
      9. Literal character: a, b
      10. Lookahead assertion: (?=...), (?!...)
      11. Lookbehind assertion: (?<=...), (?<!...)
      12. Named backreference: \k<name>
      13. Named capturing group: (?<name>...)
      14. Non-capturing group: (?:...)
      15. Quantifier: *, +, ?, {n}, {n,}, {n,m}
      16. Unicode character class escape: \p{...}, \P{...}
      17. Wildcard: .
      18. Word boundary assertion: \b, \B
      1. 概述
      2. Error: Permission denied to access property "x"
      3. InternalError: too much recursion
      4. RangeError: argument is not a valid code point
      5. RangeError: BigInt division by zero
      6. RangeError: BigInt negative exponent
      7. RangeError: invalid array length
      8. RangeError: invalid date
      9. RangeError: precision is out of range
      10. RangeError: radix must be an integer
      11. RangeError: repeat count must be less than infinity
      12. RangeError: repeat count must be non-negative
      13. RangeError: x can't be converted to BigInt because it isn't an integer
      14. ReferenceError: "x" is not defined
      15. ReferenceError: assignment to undeclared variable "x"
      16. ReferenceError: can't access lexical declaration 'X' before initialization
      17. ReferenceError: deprecated caller or arguments usage
      18. ReferenceError: reference to undefined property "x"
      19. SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
      20. SyntaxError: "use strict" not allowed in function with non-simple parameters
      21. SyntaxError: "x" is a reserved identifier
      22. SyntaxError: a declaration in the head of a for-of loop can't have an initializer
      23. SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
      24. SyntaxError: await is only valid in async functions, async generators and modules
      25. SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions
      26. SyntaxError: continue must be inside loop
      27. SyntaxError: for-in loop head declarations may not have initializers
      28. SyntaxError: function statement requires a name
      29. SyntaxError: getter and setter for private name #x should either be both static or non-static
      30. SyntaxError: identifier starts immediately after numeric literal
      31. SyntaxError: illegal character
      32. SyntaxError: invalid assignment left-hand side
      33. SyntaxError: invalid BigInt syntax
      34. SyntaxError: invalid regular expression flag "x"
      35. SyntaxError: JSON.parse: bad parsing
      36. SyntaxError: label not found
      37. SyntaxError: missing ; before statement
      38. SyntaxError: missing : after property id
      39. SyntaxError: missing ) after argument list
      40. SyntaxError: missing ) after condition
      41. SyntaxError: missing ] after element list
      42. SyntaxError: missing } after function body
      43. SyntaxError: missing } after property list
      44. SyntaxError: missing = in const declaration
      45. SyntaxError: missing formal parameter
      46. SyntaxError: missing name after . operator
      47. SyntaxError: missing variable name
      48. SyntaxError: redeclaration of formal parameter "x"
      49. SyntaxError: return not in function
      50. SyntaxError: test for equality (==) mistyped as assignment (=)?
      51. SyntaxError: Unexpected '#' used outside of class body
      52. SyntaxError: Unexpected token
      53. SyntaxError: unlabeled break must be inside loop or switch
      54. SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'
      55. SyntaxError: unterminated string literal
      56. SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
      57. TypeError: 'x' is not iterable
      58. TypeError: "x" has no properties
      59. TypeError: "x" is (not) "y"
      60. TypeError: "x" is not a constructor
      61. TypeError: "x" is not a function
      62. TypeError: "x" is not a non-null object
      63. TypeError: "x" is read-only
      64. TypeError: can't assign to property "x" on "y": not an object
      65. TypeError: can't convert BigInt to number
      66. TypeError: can't convert x to BigInt
      67. TypeError: can't define property "x": "obj" is not extensible
      68. TypeError: can't delete non-configurable array element
      69. TypeError: can't redefine non-configurable property "x"
      70. TypeError: cannot use 'in' operator to search for 'x' in 'y'
      71. TypeError: cyclic object value
      72. TypeError: invalid 'instanceof' operand 'x'
      73. TypeError: invalid Array.prototype.sort argument
      74. TypeError: invalid assignment to const "x"
      75. TypeError: More arguments needed
      76. TypeError: property "x" is non-configurable and can't be deleted
      77. TypeError: Reduce of empty array with no initial value
      78. TypeError: setting getter-only property "x"
      79. TypeError: X.prototype.y called on incompatible type
      80. URIError: malformed URI sequence
      81. Warning: -file- is being assigned a //# sourceMappingURL, but already has one
      82. Warning: unreachable code after return statement
      默认情况下,像 * 和 + 这样的量词是“贪婪的”,这意味着它们试图匹配尽可能多的字符串。?量词后面的字符使量词“非贪婪”:意思是它一旦找到匹配就会停止。例如,给定一个字符串“some <foo> <bar> new </bar> </foo> thing”: /<.*>/ will match "<foo> <bar> new </bar> </foo>"
    31. /<.*?>/ will match "<foo>"

    举例说明

    重复模式

    js

    var wordEndingWithAs = /\w+a+/;
    var delicateMessage = "This is Spartaaaaaaa";
    console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
    

    计算字符集

    js

    var singleLetterWord = /\b\w\b/g;
    var notSoLongWord = /\b\w{1,6}\b/g;
    var loooongWord = /\b\w{13,}\b/g;
    var sentence = "Why do I have to learn multiplication table?";
    console.table(sentence.match(singleLetterWord)); // ["I"]
    console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "I", "have", "to", "learn", "table" ]
    console.table(sentence.match(loooongWord)); // ["multiplication"] 可选可选字符
    

    可选字符

    js

    var britishText = "He asked his neighbour a favour.";
    var americanText = "He asked his neighbor a favor.";
    var regexpEnding = /\w+ou?r/g;
    // \w+ One or several letters
    // o   followed by an "o",
    // u?  optionally followed by a "u"
    // r   followed by an "r"
    console.table(britishText.match(regexpEnding));
    // ["neighbour", "favour"]
    console.table(americanText.match(regexpEnding));
    // ["neighbor", "favor"]
    

    贪婪 与 非贪婪的

    js

    var text = "I must be getting somewhere near the centre of the earth.";
    var greedyRegexp = /[\w ]+/;
    // [\w ]      a letter of the latin alphabet or a whitespace
    //      +     one or several times
    console.log(text.match(greedyRegexp)[0]);
    // "I must be getting somewhere near the centre of the earth"
    // almost all of the text matches (leaves out the dot character)
    var nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
    console.log(text.match(nonGreedyRegexp));
    // "I"
    // The match is the smallest one possible
      
  • Regular expressions guide
  • Character classes
  • Assertions
  •  
    推荐文章
    谦逊的茴香  ·  【读史】正始十年,司马懿的微醉与梦回-时政频道-新华网
    1 年前
    逃课的硬币  ·  可视化(Visualization)和图形学(Graphics)的区别和联系是什么 ...
    1 年前
    爱笑的紫菜汤  ·  全世界都有点意外,俄乌战场突然传来一个好消息_手机新浪网
    1 年前
    挂过科的板栗  ·  新一代互联网数字经济创新发展论坛暨第一届Web3.0信息与运营管理 ...
    1 年前
    千年单身的红薯  ·  小米手表color蓝牙怎么开启-百度经验
    1 年前
    Link管理   ·   Sov5搜索   ·   小百科
    小百科 - 百科知识指南