4th September 2019

# Word boundary: \b

A word boundary `\b` is a test, just like `^` and `\$`.

When the regexp engine (program module that implements searching for regexps) comes across `\b`, it checks that the position in the string is a word boundary.

There are three different positions that qualify as word boundaries:

• At string start, if the first string character is a word character `\w`.
• Between two characters in the string, where one is a word character `\w` and the other is not.
• At string end, if the last string character is a word character `\w`.

For instance, regexp `\bJava\b` will be found in `Hello, Java!`, where `Java` is a standalone word, but not in `Hello, JavaScript!`.

``````alert( "Hello, Java!".match(/\bJava\b/) ); // Java
alert( "Hello, JavaScript!".match(/\bJava\b/) ); // null``````

In the string `Hello, Java!` following positions correspond to `\b`:

So, it matches the pattern `\bHello\b`, because:

1. At the beginning of the string matches the first test `\b`.
2. Then matches the word `Hello`.
3. Then the test `\b` matches again, as we’re between `o` and a space.

The pattern `\bJava\b` would also match. But not `\bHell\b` (because there’s no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `\w`, so there’s no word boundary after it).

``````alert( "Hello, Java!".match(/\bHello\b/) ); // Hello
alert( "Hello, Java!".match(/\bJava\b/) );  // Java
alert( "Hello, Java!".match(/\bHell\b/) );  // null (no match)
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (no match)``````

We can use `\b` not only with words, but with digits as well.

For example, the pattern `\b\d\d\b` looks for standalone 2-digit numbers. In other words, it looks for 2-digit numbers that are surrounded by characters different from `\w`, such as spaces or punctuation (or text start/end).

``````alert( "1 23 456 78".match(/\b\d\d\b/g) ); // 23,78
alert( "12,34,56".match(/\b\d\d\b/g) ); // 12,34,56``````
Word boundary `\b` doesn’t work for non-latin alphabets

The word boundary test `\b` checks that there should be `\w` on the one side from the position and "not `\w`" – on the other side.

But `\w` means a latin letter `a-z` (or a digit or an underscore), so the test doesn’t work for other characters, e.g. cyrillic letters or hieroglyphs.

### Find the time

The time has a format: `hours:minutes`. Both hours and minutes has two digits, like `09:00`.

Make a regexp to find time in the string: `Breakfast at 09:00 in the room 123:456.`

P.S. In this task there’s no need to check time correctness yet, so `25:99` can also be a valid result.

P.P.S. The regexp shouldn’t match `123:456`.

The answer: `\b\d\d:\d\d\b`.

``alert( "Breakfast at 09:00 in the room 123:456.".match( /\b\d\d:\d\d\b/ ) ); // 09:00``
Tutorial map

• To insert a few words of code, use the `<code>` tag, for several lines – use `<pre>`, for more than 10 lines – use a sandbox (plnkr, JSBin, codepen…)
{"ft_lang_label":"__label__en","ft_lang_prob":0.74537534,"math_prob":0.9156239,"size":2283,"snap":"2019-35-2019-39","text_gpt3_token_len":639,"char_repetition_ratio":0.1509434,"word_repetition_ratio":0.05927835,"special_character_ratio":0.29610163,"punctuation_ratio":0.17177914,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9636078,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-16T16:40:10Z\",\"WARC-Record-ID\":\"<urn:uuid:7c0eaf78-1944-4a30-aca1-87ff38527cc4>\",\"Content-Length\":\"26800\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f527e863-0a59-4ca7-b100-efe10698743e>\",\"WARC-Concurrent-To\":\"<urn:uuid:3c0aeb03-5d4b-4aab-adf6-12ed9cc8db7e>\",\"WARC-IP-Address\":\"104.25.125.98\",\"WARC-Target-URI\":\"http://javascript.info/regexp-boundary\",\"WARC-Payload-Digest\":\"sha1:CUT4P25BLGPAFP6MMQ6M6PSBFG7XPJGB\",\"WARC-Block-Digest\":\"sha1:DZTZTQKDKJAIUDO6T32FFGFDWDRSHAAV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514572879.28_warc_CC-MAIN-20190916155946-20190916181946-00269.warc.gz\"}"}