1

I have string

var myString = 'Hello word Sentence number';

I need make array like ["Hello ","word ","Sentence ","number "] with space after words, I did myString.split(/(?= )/); but I received ["Hello"," word"," Sentence"," number"] How to make that space was after words?

I get this line during the event "keyup" the space is a sign that the word over and it can be compared. I don't need .trim()

  • 1
    May I ask why you want to retain the spaces? – Sampson Jun 24 '15 at 18:33
  • 1
    I don't think there's a way to do it using just the `split` function. Your example is a little unclear because you add a space to the last element, `number`, even though there's not a space after it in your original string. You can always just loop through the array and manually add a space to each element. – mittmemo Jun 24 '15 at 18:33
  • 1
    there is no space after the word 'number' but in the resulting array the word is followed by a space as well ... is that what you want ? if so you could split your string and then use the 'map' function on the result to append a space to all strings. – André R. Jun 24 '15 at 18:35
  • I have another array with spaces and I compare with it – Svetlana Konstantinovna Ivanni Jun 24 '15 at 18:39
  • 1
    @SvetlanaKonstantinovnaIvanni maybe try getting rid of all the spaces in both arrays using `.trim()` that way you don't have to worry about the spaces at all... – brso05 Jun 24 '15 at 18:42
  • I get this line during the event "keyup" the space is a sign that the word over and it can be compared. I don't need .trim() – Svetlana Konstantinovna Ivanni Jun 24 '15 at 19:02
  • @SvetlanaKonstantinovnaIvanni : have you solved the problem? – Moppo Jun 25 '15 at 13:15

5 Answers5

1

Not quite what you were after, but you can split on word boundaries using the regex token \b. Here's a fiddle: https://jsfiddle.net/930xy5b7/

myString.split(/\b/);
CodeHunter
  • 11
  • 3
1

A foreach loop to go through each item in the array would work:

`

var myString = 'Hello word Sentence number';
var myStringArray = myString.split(" ");

myStringArray.forEach(function(entry) {
    entry += " "
});

`

more on foreach: For-each over an array in JavaScript?

Community
  • 1
  • 1
Snappawapa
  • 1,697
  • 3
  • 20
  • 42
0

You can go back after splitting and "manually" add a space to each word, like this:

var myString = 'Hello word Sentence number';
var splitArray = myString.split(" ");

for(var x = 0; x < splitArray.length; x++){
  splitArray[x] = splitArray[
}
Moose
  • 103
  • 4
0

If you use jquery, you could use map:

//this is what you've done
var myString = 'Hello word Sentence number';
var arr = myString.split(/(?= )/);

//this will add a space in the end of every string of the array
var arrWithSpaces = $.map( arr, function(el){
  return el + " ";
});

Or, as suggested, use Array.prototype.map if you're not using jquery:

//this is what you've done
var myString = 'Hello word Sentence number'; 
var arr = myString.split(/(?= )/);

//this will add a space in the end of every string of the array
var arrWithSpaces = arr.map( function(el){ 
    return el + " "; 
});
Moppo
  • 18,797
  • 5
  • 65
  • 64
0

ES6 map function:

const myString = 'Hello word Sentence number';
const splitString = myString.split(' ').map(el => `${el} `);
console.log(splitString);
Ali Klein
  • 1,811
  • 13
  • 13
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Reinstate Monica Oct 15 '18 at 17:56