You can provide a regex for split () to separate a comma or underscore, use the following:
var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all"; var myarray = mystring.split(/[,_]/);
If you are after something more dynamic, you can try something like "Search and not replace," using the replace () function to parse a complex string. For example,
mystring.replace(/(?:^|,)([^_]+)_([^_]+)_([^_]+)(?:,|$)/g, function ($0, first, second, third) { // In this closure, `first` would be "mystring", // `second` would be the following number, // `third` would be "all" or "mine" });
Andy e
source share