2014-03-24 71 views
-1

我需要在javascript中使用正則表達式模式來匹配下面類型的字符串。與此字符串匹配的正則表達式

E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen> 

所以這個單一的模式應與這兩個字符串:

1. /this/is/single-word 
2. /this/is more-than/single/word-patterns/to-be-matched 

只有斜槓(/)和「本」在開始的時候是一致的,並且只包含字母。

+0

'test'in beginning?你的意思是'這個'?此外,它看起來像第二個例子不匹配的模式,因爲有一個沒有連字符的單詞的部分。 – JLewkovich

+2

有什麼不同:http://stackoverflow.com/questions/22616534/regex-pattern-to-match-a-type-of-strings – anubhava

+0

@anubhava ...字符串實際上包含連字符而不是空格。我在前面的問題中輸入了錯誤的字符串 –

回答

1

有你的問題有些不一致,這是不是很清楚要匹配什麼。

這就是說,以下正則表達式將爲您想要的確切字符串提供一個鬆散的起點。

/this/(?:[\w|-]+/?){1,10} 

這裏假設你的網址中的''並不是有意的。這個例子會將URL與'/ this /'+ 1匹配到10個額外的'/'塊。

(?:)  -> non-matching group 
[\w|-]+ -> one or more word characters or a hyphen 
/?  -> zero or one slashes 
{1,10} -> 1 to 10 of the previous element, the non-matching group 
相關問題