Harry
Harry

Reputation: 489

How to split words using javascript

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

Upvotes: 13

Views: 35520

Answers (7)

Rivenfall
Rivenfall

Reputation: 1263

I found a list of word separators in Sublime Text default settings. Here's how to split with it, with some Unicode support (the defined separators are not Unicode though):

{ // word_separators: ./\()"'-,;<>~!@#$%^&*|+=[]{}`~?: (32)
    function splitByWords(str = '', limit = undefined) {
        return str.split(/[-./\\()"',;<>~!@#$%^&*|+=[\]{}`~?:]/u, limit)
    }

    function reverseString(str) {
        let newString = ''
        for (let i = str.length - 1; i >= 0; i--)
            newString += str[i]
        return newString
    }

    const str = '123.x/x\\x(x)x"x\'x-x:x,789;x<x>x~x!x@x#x$x%x^x&x*x|x+x=x[x]x{x}x`x~x?456'
    console.log(splitByWords(str)) // (33) ["123", "x", "x", "x", "x", "x", "x", "x", "x", "x", "789", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "456"]
    console.log(splitByWords(str, 1)) // ["123"]
    console.log(splitByWords(reverseString(str), 1)) // ["654"]
}

For some reason the - has to be at the beginning, and the : at the end. Edit: you might want to add \s (after the -) to count whitespace as separator

Upvotes: 3

ESL
ESL

Reputation: 1016

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>

Upvotes: 4

r0skar
r0skar

Reputation: 8696

I think you could do it like this:

var a= 'even,'
var newA = a.slice(0, -1)

This will remove the last char from a given string.

And to check if the string contains a comma, I would do the following:

if (a.indexOf(",") >= 0){
    //contains a comma
} else {
    //no comma
}

I am a javascript beginner, so this probably is not the best way to do it, but nevertheless it works.

Upvotes: 1

v100ev
v100ev

Reputation: 63

a.split(',')

or

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

Upvotes: 1

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34581

Just use this code:

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

Upvotes: 1

Spudley
Spudley

Reputation: 168655

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.

Upvotes: 15

megakorre
megakorre

Reputation: 2223

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

Upvotes: 0

Related Questions