1

can i give more than one parameter to the split function in javascript to remove more than one thing and put it into array? for expamle:

 var str= "This is Javascript String Split Function example ";

 var wordArray=str.split(" ");

this will give me each word separately

in addition of this result, i want to remove "is" and "this" from the array, so can i put in the split function many parameter to remove from the beginning " " & "is" & "this" ? or should i use another function than split() ?

moribvndvs
  • 42,191
  • 11
  • 135
  • 149
elianore
  • 319
  • 2
  • 5
  • 9

3 Answers3

1

there are many ways to achieve what you want. Basically you want to remove is and this

so either you can do this:

a. If words you want to remove are always the first and the second one then

 var filteredArray = str.split(" ").splice(2);

b. If words you want to remove can be anywhere in the string

var  filteredArray = str.replace(/this|is/g, function(w){

        switch(w){
           case 'this':
                 return '' ;

              case 'is':
                   return '';
          }

    }).trim().split(' ');

c. There is one more lame way to do this.

var str= "This is Javascript String Split Function example ";
var parts = str.split(' ');
var clean = [];
for (var i = 0; i < parts.length; i++) {
   var part = parts[i];
   if (part !== "This" && part !=="is")
      clean.push(part);
}

d. using simple regex to replace multiple words

var str= "This is Javascript String Split Function example ";
   var words = ['This', 'is'];
   var text = str.replace(new RegExp('(' + words.join('|') + ')', 'g'), '').split(' '); 

based on your need you can go for any one of them.

references : this , this and this

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
0

I wouldn't try to use split to remove things from a string. You should use "replace" to remove them, then split after.

var wordArray = str.replace(/is|this/g, '').split(" ");

That should remove "is" and "this" and then you can split it from there.

Update

Tweaked the regex just a bit. I forgot to add a g to make it get rid of all of them.

If you want it to be case-insensitive (i.e., match This, Is, etc.), add the i switch after the g.

FIDDLE

Community
  • 1
  • 1
samanime
  • 25,408
  • 15
  • 90
  • 139
  • That is not a correct regexp for the OP’s example, you’ll end up with `["Th", "is", ...` – David Hellsing Dec 11 '12 at 10:55
  • @samanime thank you!! but it didn't remove both **"is"** and **"this"** , it removes one of them only – elianore Dec 11 '12 at 11:15
  • Updated to add the g tag so it gets rid of all of them. I'm not sure why David thinks it'll get what he said it would. It seems to work just fine in jsfiddle. – samanime Dec 11 '12 at 16:30
  • @samanime thank you!! but i didn't understand how can i make it case-insensitive? can you plzzz write it here for me :) – elianore Dec 12 '12 at 09:05
  • var wordArray = str.replace(/is|this/gi, '').split(" "); – samanime Dec 12 '12 at 18:23
0

String.split takes two arguments, a separator and a limit. So basically you can remove entries from the end of the resulting array (limit the array), but not from the beginning.

Read more here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split

However, you can chain the resulting array with other array prototypes, so you can easily add a Array.splice to do what you want:

var wordArray = str.split(" ").splice(2);

If you want to specifically remove certain words, use Array.filter:

var wordArray = str.split(' ').filter(function(word) {
    return !/(is|this)/i.test(word);
});
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • thank you! but if **"is"** & **"this"** are not in the beginning if it is for example one in the beginning and one in the middle what should i do? – elianore Dec 11 '12 at 11:34