alex alex - 3 years ago 146
HTML Question

How to select the first character of the next word in a textarea?

Right now, I'm selecteing the first character (a) of the first word inside the text area like this:

<textarea ref="inputEl">abc def ghi</textarea>


methods: {
setInitialCursorPosition () {
const inputEl = this.$refs.inputEl
inputEl.focus()
inputEl.setSelectionRange(0, 1)
}
},

mounted () {
this.$nextTick(() => {
this.setInitialCursorPosition()
})
}
}


How to do it so I select the first character of the next word inside the same text area (d and so on)? Pretty much like the
w
key in the Vim editor.

Answer Source

var str     = document.getElementById("textarea").value;
var match = str.match(/\b(\w)/g);              
var first = match.join('');  

console.log(first[1]);
<textarea id="textarea" ref="inputEl">abc def ghi</textarea>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download