Skip to content Skip to sidebar Skip to footer

Regex Pattern To Match A Type Of Strings

I need to match the below type of strings using a regex pattern in javascript. E.g. /this//

Solution 1:

You can use:

\/this\/[a-zA-Z ]+\/[a-zA-Z ]+\/[a-zA-Z ]+

Working Demo


Solution 2:

I think you want something like this maybe?

(\/this\/(\w+\s?){1,}\/\w+\/(\w+\s?)+)

break down:

\/     # divder
 this  # keyword
\/     # divider
(      # begin section
 \w+   # single valid word character
 \s?   # possibly followed by a space
)      # end section
{1,}   # match previous section at least 1 times, more if possible.
\/     # divider
\w+    # single valid word character
\/     # divider
(      # begin section
 \w+   # single valid word character
 \s?   # possible space
)      # end section

Working example


Solution 3:

This might be obvious, however to match each pattern as a separate result, I believe you want to place parenthesis around the whole expression, like so:

(\/[a-zA-Z ]+\/[a-zA-Z ]+\/[a-zA-Z ]+\/[a-zA-Z ]+)

This makes sure that TWO results are returned, not just one big group.

Also, your question did not state that "this" would be static, as the other answers assumed... it says only the slashes are static. This should work for any text combo (no word this required).

Edit - actually looking back at your attempt, I see you used /this/ in your expression, so I assume that's why others did as well.

Demo: http://rubular.com/r/HGYp2qtmAM


Post a Comment for "Regex Pattern To Match A Type Of Strings"