代码之家  ›  专栏  ›  技术社区  ›  Ben

javascript regex帮助

  •  0
  • Ben  · 技术社区  · 15 年前

    我有以下的javascript regex…

    .replace("/^(a-zA-Z\-)/gi", '');
    

    还没有完成…事实上,这是错误的。本质上,我需要做的是取一个类似“xj fc-x35(1492)”的字符串,去掉()及其内容和括号前的空白。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Sean Vieira    15 年前
    replace(/^(.*?)\s?\(([^)]+)\)$/gi, "$1$2")
    

    $2

    ^   // From the start of the string
    (   // Capture a group ($1)
    .*? // That contains 0 or more elements (non-greedy)
    )   // Finish group $1
    \s? // Followed by 0 or 1 whitespace characters
    \(  // Followed by a "("
    (   // Capture a group ($2)
    [   // That contains any characters in the following set
    ^)  // Not a ")"
    ]+  // One or more times
    )   // Finish group $2
    \)$ // Followed by a ")" followed by the end of the string.
    
        2
  •  2
  •   Andrew    15 年前

    x = "XJ FC-X35 (1492)"
    x.replace(/\s*\(.*?\)/,'');