GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Modul 27 von 33 · Medien

Boundary

Kostas Diakogiannis

Boundary

The boundary metacharacter

Take a close look to the string in the picture above and the regular expression pattern that we have defined below. Normally, and if you ignore this weird \b thing, that we have currently no idea of for now, you should expect that this log returns true, because the 'cat' string is included as a part of the string in the 'catwoman' word. Thus we get a partial match, which evaluates into a match. 

This is a very rational thought, since there is no ^ caret sign before, and  no $ dollar sign after. But this \b kinda acts like so, but not  totally. What is it then?

The boundary metacharacter matches only the cat (which is between the metacharacters) only if the word is completely on it's own and no part of another one (catwoman in this case). The first \b means that there is nothing before cat in the same word, which is true, the problem is the second \b in the pattern, because that means that there should be also nothing after the 't'. The only acceptable thing would be either a space or the end of the string. 

Another example would be if we had the string 'I am the luckiest person in the world!'.

And the regex is /\bluck\b/i. In this case this evaluates to false.

Bear in mind though:  /\bluck/i evaluates also to true. 

But : /luck\b/i  evaluates to false. 

\b means that nothing should be either before or after or both in the same word.

Anleitung

Um im Kurs weiterzukommen, sende einen Versuch dieses Quiz ab. Du kannst ihn nach der letzten Frage absenden.

Zum gewählten Modul

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck