I have dynamic values in a variables, each separated with [ and ] eg
var data="[Continuing] [Returning] "; or
var data="[ACCT_BBA] "; or
var data="[12001] [12009] [21077] [13880] ";
var data="[13880] ";
Is there a way to use the split function to extract the values between the [ and the ] from above?
var arr= data.split("<what goes here?>");
eg on the last example to retrieve: 12001, 12009, 21077, 13880
data.slice(1, -2).split("] [")
should do the job, or if your start and end are uncertain maybe
data.replace(/^\s*\[|\]\s*$/g, "").split("] [")
Alternatively, if you need something more complex, the choice is usually .match
with a global regex, or building your own parser if you need to handle arbitrarily nested structures.
Use
data.split('] [').map(function (item) { return item.replace("]", "").replace("[", "")})
like this:
//var data="[Continuing] [Returning]"; // var data="[ACCT_BBA]"; var data="[12001] [12009] [21077] [13880]"; var res = data.split('] [').map(function (item) { return item.replace("]", "").replace("[", "")}) console.log(res)
What about data.match(/\\[(\\w+)\\]/g).map(e => e.slice(1, -1))
And you can replace \\w
with the scope of character, like [a-zA-Z0-9_]
Yes if you do:
var data="[12001] [12009] [21077] [13880]";
var arr = data.split(" ");
for(let i = 0; i < arr.length; i++){
arr[i] = arr[i].replace('[','');
arr[i] = arr[i].replace(']','');
}
console.log(arr);
just an example, very basic one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.