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() ?

3 Answers 3

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

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

2 Comments

The only one of these three examples that will work is the first.
@David my apologies, last one, it was split by comma, anyways they work now.
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);
});

1 Comment

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?
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

5 Comments

That is not a correct regexp for the OP’s example, you’ll end up with ["Th", "is", ...
@samanime thank you!! but it didn't remove both "is" and "this" , it removes one of them only
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 thank you!! but i didn't understand how can i make it case-insensitive? can you plzzz write it here for me :)
var wordArray = str.replace(/is|this/gi, '').split(" ");

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.