Aleksandr
Aleksandr

Reputation: 437

Regular expression. Find three words in the field

Help make the correct regular expression for the search 3 words into the field. So far I have done so, but I think it's crazy.

var inp = document.getElementsByTagName('input')[0],
    button = document.getElementsByTagName('button')[0];

button.onclick = function() {
  console.log(inp.value.match(/^([а-яa-z0-9]+ ){2}[а-яa-z0-9]+/i));
};
<input type="text" />
<button>Check</button>

Upvotes: 0

Views: 75

Answers (1)

ralfstx
ralfstx

Reputation: 4023

I guess it's easier to split the text and then verify that the element count is as you expect it. You may want to trim the text before to avoid leading and trailing empty strings in the result array.

console.log(inp.value.trim().split(/\s+/))

Upvotes: 2

Related Questions