I am trying to detect the full word the user is tapping when they focus in on an HTML input element, and then see if it is a URL. I am using the following method:
let el = this.reminderInput.nativeElement
var fullText = el.value;
console.log(fullText) //logs 'test http://www.google.com'
var split = el.selectionStart
console.log(split) //logs 18
var a = fullText.substring(0, split).match(/[A-Za-z]*$/)[0]
var b = fullText.substring(split).match(/^[A-Za-z]*/)[0]
I have the following string in my input element:
test http://www.google.com
In this example, if the user clicked in between the o
's in google, a
will print out go
and b
will print out gle
.
I'd like for a
to print out http://www.go
and b
to print out ogle.com
, and thus, when I combing a + b
, I'd get the full string http://www.google.com
and check if it is a valid URL, and if so, open it in another tab.
How do I update my regex to only detect if the word is not a whitespace/return so I can get the full word of the selection and check if it is a URL?