0

在这里,我试图弄清楚正则表达式。我创建了这个正则表达式:

a.match( /(@|#)(.*?)(\s|$|\:)/g )

它匹配推文中的所有用户和标签。问题是他们返回条件(@|#)和(\s|$|\:)

有可能不退货吗?

我正在使用 Javascript

var a ='RT @OLMJanssen: Met #FBKGames en @Jmvanhalst volop in voorbereiding: 6 juni seminar kwaliteitsborging van #sportaccommodatie bij regiseerende gemeente'
a.match( /(@|#)(.*?)(\s|$|\:)/g )
//returns ["@OLMJanssen:", "#FBKGames ", "@Jmvanhalst ", "#sportaccommodatie "]
4

3 回答 3

4

怎么样:

a.match(/[@#](\S+)(?:\s|:|$)/g)

解释:

The regular expression:

(?-imsx:[@#](\S+)(?:\s|:|$))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  [@#]                     any character of: '@', '#'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    :                        ':'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-05-21T14:59:17.047 回答
1

这应该可以解决问题:/[@#]([^\s$:]+)/g

于 2013-05-21T15:01:06.020 回答
0

用你所拥有的(即一个小组而不是一个班级)

var match, re = /(@|#)(.*?)(\s|$|\:)/g;
while (match = re.exec(a)) {
 alert(match[2]); // match[1] is "#" or "@"
}
于 2013-05-21T15:02:18.657 回答