RegExp:忽略以特定字符集开头的所有链接

时间:2015-05-20 18:24:53

标签: regex actionscript-3

我使用下面的RegExp查找字符串中的所有链接。如何添加忽略所有以这些字符之一开头的链接的条件:._ -? (例如; .sub.example.com-example.com) AS3:

var str = "hello world .sub.example.com foo bar -example.com lorem http://example.com/test";  
var filter:RegExp = /((https?:\/\/|www\.)?[äöüa-z0-9]+[äöüa-z0-9\-\:\/]{1,}+\.[\*\!\'\(\)\;\:\@\&\=\$\,\?\#\%\[\]\~\-\+\_äöüa-z0-9\/\.]{2,}+)/gi

var links = str.match(filter)
if (links !== null) {
trace("Links: " + links);
}

2 个答案:

答案 0 :(得分:0)

您可以使用以下正则表达式:

\b((https?:\/\/|www\.)?(?<![._ -])[äöüa-z0-9]+[äöüa-z0-9\-\:\/]{1,}+\.[\*\!\'\(\)\;\:\@\&\=\$\,\?\#\%\[\]\~\-\+\_äöüa-z0-9\/\.]{2,}+)\b

编辑:

  • 添加了单词边界\b
  • [._ -]添加负面外观,即(?<![._ -])

答案 1 :(得分:0)

这是我用来查找全文的正则表达式:

/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i

正则表达式解释:

\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]

Assert position at a word boundary «\b»
Match the regex below and capture its match into backreference number 1 «(https?|ftp|file)»
   Match this alternative «https?»
      Match the character string “http” literally «http»
      Match the character “s” literally «s?»
         Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Or match this alternative «ftp»
      Match the character string “ftp” literally «ftp»
   Or match this alternative «file»
      Match the character string “file” literally «file»
Match the character string “://” literally «://»
Match a single character present in the list below «[-A-Z0-9+&@#/%?=~_|$!:,.;]*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   The literal character “-” «-»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “0” and “9” «0-9»
   A single character from the list “+&@#/%?=~_|$!:,.;” «+&@#/%?=~_|$!:,.;»
Match a single character present in the list below «[A-Z0-9+&@#/%=~_|$]»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “0” and “9” «0-9»
   A single character from the list “+&@#/%=~_|$” «+&@#/%=~_|$»