Question

I'm trying to write a regex that would detect regex in a javascript file. I've spent some time and this is what I've come up with:

\/.*?\/[a-zA-Z]?

This works fine:

/^[^0-9][2,3]$/
/^[a-z]+( +[a-z]+){2,}$/g

However it fails for something like this:

/\/.*?\/[a-zA-Z]?/g

Just to clarify my goal, I'm going to be using this regex to detect regexes in JavaScript code and color code them.

Was it helpful?

Solution

You should also check whether the / is preceeded by a \ or not to make sure it is not being escaped:

(?<!\\)\/.*?(?<!\\)\/[a-zA-Z]?

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top