As an alternative, how about something like that?
var strings = [
'hello hi @why helo @blow but @name',
'hello hi @why helo @blow but name@name',
' hello hi @why helo @blow but name@name ',
'@blow but not know how to resolve this',
' @blow but not know how to resolve this',
'tada',
' ',
''
];
var wanted = strings.map(function (element) {
var found = 'not found';
element.split(/\s+/).reverse().some(function (part) {
if (part.charAt(0) === '@') {
found = part.slice(1);
return true;
}
});
return found;
});
document.getElementById('out').textContent = wanted.join('\n')
<pre id='out'></pre>
Run codeHide resultNo complicated RegExp, easy to understand and change behavior. requires ES5 or gaskets, but not big.
source
share