Reputation: 335
I need to split a string like this
var val = "$cs+55+mod($a)";
into an array
arr = val.split( /[+-/*()\s*]/ );
The problem is to keep the splitter character as an array element like
arr = [ '$cs', '+', '55', 'mod', '(', '$a', ')' ]
and not like
arr = [ '$cs', '55', 'mod', '$a' ]
Upvotes: 1
Views: 175
Reputation: 335
1st thank you all for the help.
My problem is solved but not finished.
Here is a more detailed usage
$(function() {
var tags = [
'$c[8][clientes][clientes_iva]',
'$c[8][paises_iva][paises_iva]',
'$c[8]',
'mod(',
'$user'
];
function split( val ) {
var arr = val.split( /([+-\/*()\s*]|[^+-\/*()\s*]+)/g );
return arr;
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#condition" )
.bind("keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
response( $.ui.autocomplete.filter( tags, extractLast( request.term ) ) );
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = split(this.value);
terms.pop();
terms.push( ui.item.value );
this.value = terms.join( "" );
return false;
}
});
});
I made a Video where you all can see what I'm doing.
Upvotes: 0
Reputation: 41832
You should use match
instead of split
.
"$cs+55+mod($a)".match(/([+-/*()\s*]|[^+-/*()\s*]+)/g);
Explanation:
[+-/*()\s*] -- Your provided regex
[^+-/*()\s*]+ -- Negation of the above regex using ^ and mentioning that could be more than one letter by using +
Upvotes: 4