Home > Blockchain >  Regex: difference between ^a and a.*
Regex: difference between ^a and a.*

Time:07-25

I have been looking everywhere and I just cannot find anybody looking for the answer. what is the difference between:

'/^a/'

and

'/a.*/'

both of them say: give me everything that starts with 'a'. so what is the difference?

edit: I am not adding the regex tool since i was not trying it out on a specific one.

CodePudding user response:

/^a/ means "match the beginning of the string, and then the character a".

/a.*/ means "match the character a anywhere in the string, and then 0 or more of any other characters."

/^a/ and /a.*/ both match "apple" but only /a.*/ will match "cat".

CodePudding user response:

/^a/ marks "a" at the beginning of a string, but /a.*/ marks "a" and all following characters.

That means that /^a/ in "abc" marks only "a", but /a.*/ in the same string marks "abc". Hope it helped!

  • Related