admin管理员组

文章数量:1190758

I am trying to split up a string on /* and then to split up those segments on */

So that I can separate out all of the comments as I want this code to be able to take all of the comments out of the string and then put it back together.

The problem is though I keep getting this .append error which I am pretty sure is because I have made a silly syntax error but I am struggling to find it and any help would be greatly appreciated.

JS

contents = "if for /* else */ . = == === /* return */ function"

var start = /\/\*/gi;
var end = /\*\//gi;
var commentsRemovedSec2 = [];

var commentsRemovedSec1 = contents.split(start);
console.log(commentsRemovedSec1);

for (var i = 0; i < commentsRemovedSec1.length; i++) {
  var z = ""
  var x = commentsRemovedSec1[i]
  var y = x.split(start)
  z = y[0]
  commentsRemovedSec2.append(z);
};

console.log(commentsRemovedSec2);

I am trying to split up a string on /* and then to split up those segments on */

So that I can separate out all of the comments as I want this code to be able to take all of the comments out of the string and then put it back together.

The problem is though I keep getting this .append error which I am pretty sure is because I have made a silly syntax error but I am struggling to find it and any help would be greatly appreciated.

JS

contents = "if for /* else */ . = == === /* return */ function"

var start = /\/\*/gi;
var end = /\*\//gi;
var commentsRemovedSec2 = [];

var commentsRemovedSec1 = contents.split(start);
console.log(commentsRemovedSec1);

for (var i = 0; i < commentsRemovedSec1.length; i++) {
  var z = ""
  var x = commentsRemovedSec1[i]
  var y = x.split(start)
  z = y[0]
  commentsRemovedSec2.append(z);
};

console.log(commentsRemovedSec2);

Share Improve this question edited May 25, 2016 at 10:51 user5201742 asked May 25, 2016 at 9:46 1seanr1seanr 7062 gold badges7 silver badges18 bronze badges 2
  • 2 Append isn't an array method. However myArray.push(newItem) is – evolutionxbox Commented May 25, 2016 at 9:56
  • 1 As @evolutionxbox mentioned use commentsRemovedSec2.push(z); instead of append. – Ajinder Singh Commented May 25, 2016 at 9:58
Add a comment  | 

1 Answer 1

Reset to default 26

Unfortunately .append() isn't an Array method.

Instead use the Array method .push().

commentsRemovedSec2.push(z)

The push() method adds one or more elements to the end of an array and returns the new length of the array. MDN

本文标签: javascriptGetting error append is not a functionStack Overflow