K
K
Kusmich2015-09-14 10:36:35
JavaScript
Kusmich, 2015-09-14 10:36:35

How to get first 2 words from input?

I am new to jquery. I'm interested in how you can get the first 2 words from the input? (words can be separated by spaces or "-")

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Osher, 2015-09-14
@Kusmich

"some random string".split(/[\s\-]/).slice(0, 2); // ["some", "random"]
"some-random string".split(/[\s\-]/).slice(0, 2); // ["some", "random"]
"some random-string".split(/[\s\-]/).slice(0, 2); // ["some", "random"]
"some-random-string".split(/[\s\-]/).slice(0, 2); // ["some", "random"]

S
Sergey, 2015-09-14
@Pjeroo

arr = "a,b,c".split(',')  // массив ["a", "b", "c"]

javascript.ru/String/split

C
CheGevara, 2015-09-14
@CheGevara

If on pure JS, then look for the second occurrence of a space by brute force and take a piece up to this point.
I recommend writing the code for learning yourself =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question