#[rs](https://github.com/kirbyfan64/rs), 114 bytes
$$x=(?<!\\)((\*|_){2})
\t/
$x\1/
$x(\S(\\\2|.)*?)?\1/(\n)^^((^^\3))\3
\\(\*|_)/\t
[^\t\n]/
\n/_
\t_?/
(_*)/(^^\1)
[Live demo and test cases.](http://kirbyfan64.github.io/rs/index.html?script=%24%24x%3D%28%3F%3C!%5C%5C%29%28%28%5C*%7C_%29%7B2%7D%29%0A%5Ct%2F%C2%A0%0A%24x%5C1%2F%0A%24x%28%5CS%28%5C%5C%5C2%7C.%29*%3F%29%3F%5C1%2F%28%5Cn%29%5E%5E%28%28%5E%5E%5C3%29%29%5C3%0A%5C%5C%28%5C*%7C_%29%2F%5Ct%0A%5B%5E%5Ct%5Cn%5D%2F%0A%5Cn%2F_%0A%5Ct_%3F%2F%0A%28_*%29%2F%28%5E%5E%5C1%29&input=The%20**quick%20brown%20fox%20jumps%20over%20the%20lazy**%20dog.%0AThe%20**quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog.%0AThe%20__quick**__%20brown%20**fox__%20jumps**%20over__%20the__%20lazy%20**dog.%0AThe%20__quick%5C____%20brown%20fox%20**jumps%20over%5C**%20the**%20lazy%20%5C**dog.%0AThe****quick%20brown%20fox****jumps%20over%20the%20lazy%20dog.)
This is a pretty crazy...thing.
##Explanation
$$x=(?<!\\)((\*|_){2})
Create a macro that matches a set of delimiters (e.g. `**` but not `\**`).
\t/
Replace tabs with spaces. They have the same character count, and tabs are used later on as a special character.
$x\1/
Remove all empty delimiters like `****`.
$x(\S(\\\2|.)*?)?\1/(\n)^^((^^\3))\3
Replace any text of length `N` that should be bolded
with `N` newlines followed by the original text.
\\(\*|_)/\t
Replace any occurrences of a delimiter immediately preceded by a slash with a tab. This is to make sure that entries like `**a\***` have a character count of 2 instead of 3.
[^\t\n]/
Remove any character that's not a tab or newline.
\n/_
Replace all the newlines with underscores.
\t_?/
Remove any tabs (which represent escaped delimiters), along with any underscores that may follow them. This is related to the above issue of character counts with escaped ending delimiters.
(_*)/(^^\1)
Replace the underscore sequence with its length. This is the character count.