1

This code

var i = '[0]';
i.replace(/\[|\]/, '');

returns i = '0]'

I want to use .replace function to converti = '0';

6
  • 2
    Use the g flag. Commented Jul 26, 2017 at 10:29
  • it works:) please post an answer, so I can choose you. Does it mean that without 'g' it just finds the first match only? Commented Jul 26, 2017 at 10:30
  • It finds the first occurence of /\[|\]/, which is [. I generally won’t post answers without searching for duplicate targets first. Commented Jul 26, 2017 at 10:31
  • add the g flag to expration Commented Jul 26, 2017 at 10:32
  • .replace(/[/, '').replace(/]/); Commented Jul 26, 2017 at 10:33

1 Answer 1

1

edit the code and use g flag

var i = '[0]';
i.replace(/\[|\]/g, '');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.