Reputation: 492
I am trying to achieve the following for both @
or #
.
function split(val) {
return val.split(/@/);
}
function extractLast(term) {
return split(term).pop();
}
Any help really appreciated!
Upvotes: 1
Views: 152
Reputation: 11922
Try
val.split(/@|#/g);
The |
is the regex alternation operator ie 'OR'. The g
flag makes the expression match globally (ie all instances)
See this fiddle
As Pointy notes the g
flag isn't necessary here. It is however necessary if wanting to find all matches within a string in JS regular expressions.
Upvotes: 7
Reputation: 76880
you could do
function split(val) {
return val.split(/[@#]/g);
}
Upvotes: 2