Hello, I am having trouble building a regex to match the last word from a string.
Normally it would be /\s+?(\S+)?$/ so it matches anything from the last space to the end of the string. The catch is that when I have html or bb tags it would break the code, so I need it to catch those too. Note: BB code is delimited by [ ].
There are several cases:
foo bar -> " bar"
foo <tag attrib="bar">foo</tag> -> " <tag attrib="bar">foo</tag>"
foo <tag attrib="bar">fo</tag>o -> " <tag attrib="bar">fo</tag>o"
foo f<tag attrib="bar">oo</tag> -> " f<tag attrib="bar">oo</tag>"
foo f<tag attrib="bar">o</tag> -> " f<tag attrib="bar">o</tag>o"
foo <tag attrib="bar">foo b</tag>ar foo-> " foo"
So far I made /(<[a-zA-Z]+?)??\s+?(\S+)?$/ but it won't catch the characters before the "<". This won't break the code but will break words.
My next step was /\s+?(\S<.+? >)?(\S+)?$/ but this is bad (slow) for long strings and in some cases, results in catastrophic backtracking.
Thanks in advance.