Pergunta

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.

Foi útil?

Solução

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

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

DEMO

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top