2013-08-21 31 views
-4

我必須用正則表達式找到帶有「app」的字符串。該模式必須是完全一樣用reqex查找模式

有效例子

test app 
test-app 
test app 2 
test app 3 until test app x 
test-app-beta 

無效的例子

app test 
application test 
app 2 test 
app-betaxx 

你能幫助我嗎?

+3

(因此)是不是一個地方,你可以請人編寫代碼爲您服務。你有沒有試圖自己解決這個問題?如果是這樣,你在哪裏遇到問題? – Lix

+0

我會幫你幫忙:http://regexr.com –

回答

0

那麼,「應用程序」必須在前面有一個空間或短跑?

下面的解決方案做出了這個假設。

/((?:\s|-)app)/ 

Image http://f.cl.ly/items/1y0b3F3B1x0Z0b2A0Z1z/orreMBA%202013-08-21%20kl.%2011.10.32.PNG

圖片來自Regexper

+1

你在問還是回答?如果你的答案正在做出一些假設,你應該提及它們。目前,這個「答案」看起來更像是你要求更多的信息。 – Lix

+0

@Lix - 我問是否這是模式,因爲問題只顯示應該匹配什麼,什麼不應該。當然,解決方案是基於我陳述的假設。 –

1

這將匹配所有的上市測試用例。

var tests = ['test app', 
      'test-app', 
      'test app 2', 
      'test app 3 until test app x', 
      'test-app-beta', 
      'app test', 
      'application test', 
      'app 2 test', 
      'app-betaxx']; 

// match any string that contains but doesnt start with "app" 
var regexp = /.{1,}app.*/; 

var testsLen = tests.length; 
while (--testsLen) { 

    var testee = tests[testsLen]; 
    var result = regexp.exec(testee); 

    if (result) { 
    console.log('match: ', result[0]); 
    } else { 
    console.log('no match: ', testee); 
    } 

} 

輸出:

Results