Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?=^ mean in a regexp?

I want to write regexp which allows some special characters like #-. and it should contain at least one letter. I want to understand below things also:

/(?=^[A-Z0-9. '-]{1,45}$)/i

In this regexp what is the meaning of ?=^ ? What is a subexpression in regexp?

like image 368
user2855270 Avatar asked Dec 09 '22 12:12

user2855270


1 Answers

(?=) is a lookahead, it's looking ahead in the string to see if it matches without actually capturing it

^ means it matches at the BEGINNING of the input (for example with the string a test, ^test would not match as it doesn't start with "test" even though it contains it)

Overall, your expression is saying it has to ^ start and $ end with 1-45 {1,45} items that exist in your character group [A-Z0-9. '-] (case insensitive /i). The fact it is within a lookahead in this case just means it's not going to capture anything (zero-length match).

like image 151
Dallas Avatar answered Dec 11 '22 02:12

Dallas