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

7
  • 1
    May I ask why you want to retain the spaces? Commented Jun 24, 2015 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. Commented Jun 24, 2015 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. Commented Jun 24, 2015 at 18:35
  • I have another array with spaces and I compare with it Commented Jun 24, 2015 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... Commented Jun 24, 2015 at 18:42

5 Answers 5

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?

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

Comments

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/);

3 Comments

This produces elements for each space, which is not what OP wants.
@mittmemo it might be maybe the OP didn't realize this was an option. I would recommend stating that in your answer CodeHunter...
Yeah, it doesn't quite work does it. Ah well, it's a start.
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[
}

Comments

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 + " "; 
});

1 Comment

You can use Array.prototype.map, and leave jQuery out of this.
0

ES6 map function:

const myString = 'Hello word Sentence number';
const splitString = myString.split(' ').map(el => `${el} `);
console.log(splitString);

1 Comment

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.

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.