I have a small templating system in javascrip, where the user can put tags in the form $tagname$. I can match all tags with the patter: /\$\w+\$/.
Also, I want to match incomplete tags specifically (it would start with $ and finish with a word boundary that is not $). I can’t use /\$\w+\b/ because $ is also a word boundary (so it will also match correct tags). I tried with this but it does not work: /\$\w+[^\$]/.
It matches the incomplete tag in this string “word $tag any word”, but it also matches this “word $tag$ any word”.
What is the correct ending for that regular expresion?
If you want to match incomplete tags specifically, you can use a negative lookahead:
But it’s probably more efficient to use Tomalak’s regex and do a separate check to see if it ends with
$. Or capture the (optional) ending$like this:If group #1 contains an empty string, it’s an incomplete tag.