<input id="i"> <button id="b">get</button> <script> b.onclick=function(){ alert( i.value.match(/[^ -]+/g).splice(0, 2)) } </script>