Please tell me how the expression on JS should look like in order to delete all words after the first word?

For example: "Vasya Ivanovich Pupkin". You just have to get "Vasya." Words are always different ...

  • You can find the position of the first space in the line through indexOf(' ') and delete, starting with it. You can turn a string into an array, breaking it up by the space split(' ') - and taking the zero element of the array. You can catch only the first word with a regular expression via preg_match() . Try to find documentation with examples on these methods and write at least some code. - Sergiks
  • And what have you tried to do? How did you try to solve the problem? - Alex
  • And you can not an example with a variable? There x = "word words" how next? - Alex.man.55
  • ps instead of preg_match() course, meant .match() :) - Sergiks

1 answer 1

Not necessarily something to cut. You can work through the separator and take its first element.

 var str = 'Вася Иванович Пупкин' console.log(str.split(' ')[0]); 
  • one
    This is in the first comment to the question. It is a pity that the vehicle will just copy your answer, and he will continue to resort to SO for a ready-made solution, while they give it. - Sergiks
  • @Sergiks, I can not disagree with you. And sorry, I missed your comment. Just saw the question, wrote the answer. But yes. People today are not very fond of reading docks about the languages ​​in which they want to write. - Vasily Barbashev