How to split words using javascript

Related searches

This might be a simple question but, how do i split words... for example

a = "even, test"

I have used .split to seperate the text with space.

so the result came is like

a = "even,"
b = "test"

But, how do I remove the 'comma' here?

But in some conditions it might get "even test" and in some conditions i might get "even, test". All are dynamic, so how do i check it for both?

Thanks

Firstly, the split() function is not jQuery - it is pure Javascript.

Did you try doing split with a comma and a space? That would have worked just fine in your case:

var result = input.split(', ');

For more complex splits, you can use regular expression pattern matching to allow multiple commas or spaces between the two fields:

var result = input.split(/[, ]+/);

but you probably don't need to go that far in your case.

How to split words using javascript, Omit the separator parameter: var str = "How are you doing today?"; var result = input.split(', '); For more complex splits, you can use regular expression pattern matching to allow multiple commas or spaces between the two fields: var result = input.split(/ [, ]+/); but you probably don't need to go that far in your case. share.

I think is better to use something like this:

text.match(/[a-z'\-]+/gi);

Example:

var e=function()
 {
  var r=document.getElementById('r');
  var x=document.getElementById('t').value.match(/[a-z'\-]+/gi);
  for(var i=0;i<x.length;i++)
   {
    var li=document.createElement('li');
    li.innerText=x[i];
    r.appendChild(li);  
   }
 }
<div style="float:right;width:18%">
 <ol id="r" style="display:block;width:auto;border:1px inner;overflow:scroll;height:8em;max-height:10em;"></ol>
 <button onclick="e()">Extract words!</button>
</div>
<textarea id="t" style="width:70%;height:12em">even, test; spider-man

But saying o'er what I have said before:
My child is yet a stranger in the world;
She hath not seen the change of fourteen years,
Let two more summers wither in their pride,
Ere we may think her ripe to be a bride.

—Shakespeare, William. The Tragedy of Romeo and Juliet</textarea>

javascript break sentence by words, Firstly, the split() function is not jQuery - it is pure Javascript. Did you try doing split with a comma and a space? That would have worked just� That’s where the JS split() method and slice() method come in. These functions allow you to manipulate a string to specify certain information. For example, you could use split() to divide a user’s name into two strings: one for the user’s first name, and one for their surname. Or you could use slice() to get certain characters from a larger string.

Hej Harry

if the comma is the separator you can call split with the comma

Ex:

var newColls = myString.split(",");

and not split with space.

GL

String.prototype.split(), Just use split : var str = "This is an amazing sentence."; var words = str.split(" "); console.log(words); //["This", "is", "an", "amazing", "sentence."]. str.split () function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument. The syntax of the function is as follows:

Just use this code:

var a = "even, test";
var words = a.split(", ");

JavaScript: Split a string and convert it into an array of words , JavaScript exercises, practice and solution: Write a JavaScript function to split a string and convert it into an array of words. Given a statement which contains the string and separators, the task is to split the string into substring. String split () Method: The str.split () function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.

a.split(',')

or

var re = /\s*,\s*/
var newA = a.split(re);

Instead of this pattern, you can also use \\P{L} pattern to extract words from the sentence, where \\P denotes POSIX expression and L denotes character class for word characters. You need to change the line with the split method as given below.

Let’s use string.split()function and pass a single-space string as a separator string. vartext=' Splitting String Into Tokens in JavaScript 'console.log(text.split(' ')) It gives something like: ['', 'Splitting', 'String', '', '', 'Into', '', 'Tokens', 'in', 'JavaScript', ''] So this is not exactly what we expected.

Comments
  • a.split(/\W+/).filter(x => x);
  • Does this answer your question? How do I split a string, breaking at a particular character?
  • Thanks, but in some conditions i might get "even test" and in some conditions i might get "even, test". so how do i check it for both?
  • The answer has been given to you by @Spudley. Use regular expression: a.split(/[, ]+/);
  • @Harry: Also, with mine it use any non-word text as a separator (only extracts words).