Ad

In JavaScript What Characters Is The Following Code Replacing In Mystring JS Variable?

- 1 answer

In JavaScript what characters is the following code replacing in mystring JS variable?

 var mySecondString = myFirstString.replace(/['"]+/g, '');
Ad

Answer

Breaking this down:

  • " and ' are themselves.
  • [] means "any in the set".
  • + indicates "one or more times" of the preceeding item.
  • g flag means all occurrences.

Which means it is replacing all instances of ' and " in the string to blanks. And as mentioned in the comments, + is useless since you have the g flag. Read more about RegExp in MDN.

Ad
source: stackoverflow.com
Ad