(string-match-p "^\(./\|../\)" "../lib/tern")

returns false.

What is the correct regex pattern to check if the string begins with ./ or ../?


This is because of not enough escaping. You do need to escape dots (unescaped it means "any character just not the end of line"). When you escape anything in a string literal you need double slash.

A regular expression you want could be this:

(string-match-p "^\\.\\.?/" "../lib/tern")

Your original version could be rewritten like so:

(string-match-p "^\\(:?\\.\\|\\.\\.\\)/" "../lib/tern")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.