0

本质上,我在为列表创建正则表达式时遇到问题,例如:

1. Eccentric Shop Teacher                                                       20M+         Good              EST
2. Wanwood Crown                                                                  18M+          Low                WWC
3. Domino Crown                                                                      16M+         Great              DC
4. Dominus Empyreus                                                              16M+         Great              Emp

我正在使用的正则表达式:(\d+.\s)([\w\'\s\d])([\d\w\+])

仅匹配:

  • 1:“1。”
  • 2:“E”
  • 3:“c”

但我希望它像这样匹配:

  • 1:“1。”
  • 2:《古怪的店老师》
  • 3:“20M+”
  • 4:“好”
  • 5:“美国东部标准时间”

任何有正则表达式的专家可以帮助我吗?

4

1 回答 1

0

试试这个正则表达式:

var regex = /(\d+\.)\s+([a-z]+(?:\s[a-z]+)*)\s+(\d+M\+)\s+([a-z]+)\s+([a-z]+)/i;

说明:

(\d+\.)          # first capturing group: one or more digits and a dot
\s+              # spaces

(                # open the second capturing group
  [a-z]+         # one or more letters (a word)
  (?:            # open a non capturing group
    \s[a-z]+     # a space followed by a word
  )*             # close the non capturing group, repeat zero or more times
)                # close the second capturing group

\s+              # one or more spaces
(\d+M\+)         # third capturing group: one or more digits, M, +

\s+
([a-z]+)         # fourth capturing group: a word

\s+
([a-z]+)         # fifth capturing group: a word
于 2013-07-16T10:52:23.063 回答