Split string using spaces in javascript?

I need a tokenizer that sets a string with an arbitrary space between words, creates an array of words without empty substrings.

For example, if a string is specified:

" I dont know what you mean by glory Alice said." 

I use:

 str2.split(" ") 

This also returns empty substrings:

 ["", "I", "dont", "know", "what", "you", "mean", "by", "glory", "", "Alice", "said."] 

How to filter empty lines from an array?

+8
javascript tokenize
source share
6 answers

You probably don't even need to filter, just split it with this regex:

 " I dont know what you mean by glory Alice said.".split(/\b\s+/) 
+15
source share

You must trim the string before using split.

 var str = " I dont know what you mean by glory Alice said." var trimmed = str.replace(/^\s+|\s+$/g, ''); trimmed = str.split(" ") 
+7
source share
  str.match(/\S+/g) 

returns a list of non-spatial sequences ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said."] (note that this includes a dot at the “specified”.)

  str.match(/\w+/g) 

returns a list of all words: ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said"]

docs on match ()

+7
source share

Recommend .match :

 str.match(/\b\w+\b/g); 

This corresponds to words between word boundaries, so all spaces do not match and therefore are not included in the resulting array.

+2
source share
0
source share

I think the empty substring is because there are several white spaces that you can use to replace () in a for loop to replace several white spaces with one white space and then split () to split the program using one white space like this:

 // getting full program from div var program = document.getElementById("ans").textContent; //removing multiple spaces var res = program.replace(" ", " "); for (i = 0; i <= program.length; i++) { var res = res.replace(" ", " "); } // spliting each word using space as saperator var result = res.split(" "); 
0
source share

All Articles