$
represents the end of the line, therefore ...
\b(\w+)$
However, your test string seems to have dollar sign delimiters, so if they always are, you can use this instead \b
.
\$(\w+)\$$
var s = "$this$ $is$ $a$ $test$";
document.body.textContent = /\$(\w+)\$$/.exec(s)[1];
Run codeHide resultIf there may be trailing spaces, add \s*
to the end.
\$(\w+)\$\s*$
And finally, if at the end there may be other material without a word, then use \W*
.
\b(\w+)\W*$
source
share