正则表达式模式匹配此字符串

时间:2014-03-24 18:08:08

标签: javascript regex

我需要在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

只有斜杠(/)和&#39;这个&#39;在开头是一致的,只包含字母表。

2 个答案:

答案 0 :(得分:1)

试试这个 -

^\/this(?:\/[\w\- ]+)+$

演示here

答案 1 :(得分:1)

您的问题中存在一些不一致之处,并不完全清楚您想要匹配的内容。

话虽如此,以下正则表达式将为您想要的确切字符串提供一个宽松的起点。

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

这假设您网址中的''不是故意的。此示例将匹配带有'/ 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
相关问题