2

I want to split a string by the delimiter \s and , so that 'hello , world , hi yes' returns as [ 'hello', 'world', 'hi', 'yes' ]

Right now I'm using str.split(/[\s,]+/), but the issue with this is that it doesn't compensate for falsely strings such as the empty string or a string that consists of only space.

That is, '' returns [ '' ] and ' ' returns [ '', '' ], but they should just return []

2
  • use this \w{1,} Demo Commented Feb 9, 2018 at 17:36
  • Could you please add the extra information that you provided in the answer in the question via an edit? Once you have completed this I will delete my answer as it no longer will be a valid response. Commented Feb 9, 2018 at 18:12

5 Answers 5

3

You really only need a RegEx to detect the spaces:

var s = 'hello    , world , hi, yes a b';
// Replace one or more spces with a comma
// Split on the commas
// Do a filtering loop over results
// Return any strings (into a new array) that have characters in them after a trim is performed
var result = s.replace(/\s+/g, ",").split(",").filter(word => { return word.trim() !== "" ; });
console.log(result);

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

7 Comments

I'm sorry I forgot to add that \s is a delimiter by itself as well, so ``a b'` should return ['a','b']
@thestateofmay Answer updated to address space delimiters as well.
@georg No. As you can see, you just need filter because filter also returns a new array, just like map does.
right! What about your "dont' need regex" statement now? ;)
Actually, you can do with re's at all, as in your first version: .split(',').map(x => x.trim()).filter(x => x.length)
|
1

When working with regexes, it's usually better to concentrate on what you want instead on what you don't want. In other words, use match, not split:

s = 'hello    , world , hi, yes'
r = s.match(/[^\s,]+/g)
console.log(r)


s = '    '
r = s.match(/[^\s,]+/g)
console.log(r)

If you don't like null as the "empty" result, do

r = s.match(/[^\s,]+/g) || []

Comments

0

Try this expression, that looks for zero or more spaces before and after the comma:

\s*,\s*

Demo: https://regex101.com/r/6wWQdB/1

Comments

0

You can accomplish this with a single line of JavaScript. It is really simple.

let testString = 'hello    , world , hi, yes';

let wordArray = testString.split(',').map(word => (word.trim()));

console.log(wordArray); // prints ['hello', 'world', 'hi', 'yes']

3 Comments

Best attempt at what the original question was.
Made a request for the question to be updated. Once that happens I will remove my response.
The OP seems to be happy with what they've got anyways, let's move on ;)
0

You need to specify a count of chars, for example, at least one:

'hello    , world , hi, yes'.split(/[\s,]+/)

Notice that split will always return at least one item = the entire string if no matches were found. So ''.split(/[\s,]+/) will produce [''].

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.