Jane
Jane

Reputation: 1

Get first word from strange string

I've got this string like that: var name = "\n\n\t\n\n\t\t\n\t\t\tName\n\t\t\n\n\t\t(send picture)\n\t\n" I'd like to get first word (in this example --> Name).

I've tried regex expression, but I couldn't write proper one. My try:

`first_name = name.match(/^([\w\-]+)/)`

Upvotes: 0

Views: 48

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

Use \w+ to match a alphanumeric characters with underscore

var name = "\n\n\t\n\n\t\t\n\t\t\tName\n\t\t\n\n\t\t(send picture)\n\t\n";
var matches = name.match(/\w+/g);
if (matches) {
  console.log("First word - ", matches[0]);
}

Demo

Upvotes: 3

Related Questions