Sesame Sesame - 7 months ago 23
Javascript Question

Find character pattern using regex

I am trying to find all occurrences of a special character

/
surrounded by either letters or numbers.

After many tries, I have come up with the following Regex that almost does what I need:

(?![a-z0-9])\/(?=[a-z0-9])


This works fine for these examples:

aa/aa
123/123
aa/123


However, it fails if there are two forward slashes together:

http://regexr.com/


In this case, it matches the second forward slash after
http
which I do not want.

How can I modify this Regex to meet my needs?

EDIT: I do not want to a match when two forward slashes are together. I only want to match if a single forward slash is surrounded by alphanumeric characters.

Answer

you would need a positive lookbehind group, like so:

(?<=[a-z0-9])+\/{1}(?=[a-z0-9]+)

however, according to http://regexr.com/ it is not supported in javascript.

Works fine in e.g. python http://pythex.org/