1

My split() is not working when there is a space before or after each line in my variable. The code below should return (123,45,67,89) but instead it returns (1234567,89). The REGEX is the problem, probably, but I don't know how to fix it. Your help will be appreciated. Thank you,

 var keywordsArray = "1 2 3  \n4 5\n 6       7\n8 9";
    keywordsArray = keywordsArray.replace(/\s\s+/g,' ').split('\n');
    alert(keywordsArray);

4
  • 1
    Why dont you use keywordsArray = keywordsArray.split('\n') Commented Jul 14, 2018 at 4:43
  • Your regex also replaces new lines Commented Jul 14, 2018 at 4:43
  • 1
    Thank you Aman. That's it. I had to split first before removing extra spaces. Commented Jul 14, 2018 at 4:48
  • Ok, so I have checked your code as well and the only issue with your code is that \s is replacing \n as well try using only space ' '. This will make your statement to be something like this keywordsArray = keywordsArray.replace(/ +/g,' ').split('\n'); Commented Jul 14, 2018 at 5:00

5 Answers 5

2

You can use the below approaches.

Sepearted with , (comma):

> var result = keywordsArray.split('\n')
undefined
> result
[ '1 2 3  ', '4 5', ' 6       7', '8 9' ]
>
> result = result.map(s => s.replace(/\s+/g, ''))
[ '123', '45', '67', '89' ]
>
> result.join(',')
'123,45,67,89'
>

Array of integers:

> var keywordsArray = "1 2 3  \n4 5\n 6       7\n8 9"
undefined
> keywordsArray
'1 2 3  \n4 5\n 6       7\n8 9'
>
> var result = keywordsArray.split('\n')
undefined
> result
[ '1 2 3  ', '4 5', ' 6       7', '8 9' ]
>
> result = result.map(s => parseInt(s.replace(/\s+/g, '')))
[ 123, 45, 67, 89 ]
>
Sign up to request clarification or add additional context in comments.

1 Comment

@Creek, I'm sorry for my mistake. I've updated my answer.
1

I made it worked with your string, may be this is the hard way... everything has a plan B

var keywordsArray = "1 2 3  \n4 5\n 6       7\n8 9";
keywordsArray = keywordsArray.split('\n').map(item => item.replace(/\s/g,''));
console.log(keywordsArray.join(','));

1 Comment

Thank you. This is what I was looking for.
1

Just change the regex pattern. First, remove all white space. After that, replace \n with ,.

var keywordsArray = "1 2 3  \n4 5\n 6       7\n8 9";
keywordsArray = keywordsArray.replace(/ */g,'').replace(/\n/g,',');
console.log(keywordsArray);

For more information about regex, check this link

Comments

1

The only issue with your code is that \s is matching \n as well while replace statement, so instead of \s use space (' ').

Please find your code with just update mentioned above:

var keywordsArray = "1 2 3  \n4 5\n 6       7\n8 9";
keywordsArray = keywordsArray.replace(/  +/g,' ').split('\n');
alert(keywordsArray);

Comments

-1

Try using the literal space ' ' instead of '\s' since

/\s/g any whitespace character

Check out regex101 the next time you get stuck -- it's really helpful!

Comments

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.