1

I have a string named pageList= [[],[2567,2568,],[2569,2570,2571,2537,],[2538,2586,2587,2588,],[2589,2590,2591,2592,2593,]] I need to remove the presnce of [], with (empty) and ,[] with (empty) and ,] with ] . I tried the code

              pageList = pageList.replace(/,]/g,']');                 
              pageList = pageList.replace(/[]/g,''); 
              pageList = pageList.replace(/[/]/g,'');

But its is replacing the ,] with ] but removing the ,[] and [], is not working.The output I am getting now is [[],[2567,2568],[2569,2570,2571,2537],[2538,2586,2587,2588],[2589,2590,2591,2592,2593]]

1 Answer 1

2

You do not need regex here as it is not a string.

This being an array so extra , will automatically be removed. To remove [] you can use .filter() here

var pageList=  [[],[2567,2568,],[2569,2570,2571,2537,],[2538,2586,2587,2588,],[2589,2590,2591,2592,2593,]];

pageList = pageList.filter(el => el.length!==0)

console.log(pageList);

PS: If your data is in string format then you might need to parse it to array by doing JSON.parse(pageList);

Sign up to request clarification or add additional context in comments.

1 Comment

In this code if the string is only [] it will not remove anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.