JSFiddle - React, Tailwind, and code Playground
JavaScript
var str = "Hellodkjgkjsfk jkfhsdf skfdjhskldf lsdf World!";
var pos = 2;
function getWord (str, curPos) {
var startIndex = (function _this (pos) {
if (!str.substring(pos, pos + 1).match(/[A-Za-z]/)) {
return pos + 1;
} else if (pos === 0) {
return 0;
} else {
return _this(pos - 1);
}
})(curPos - 1);
var endIndex = (function _this (pos) {
if (!str.substring(pos, pos + 1).match(/[A-Za-z]/) || pos === str.length) {
return pos;
} else {
return _this(pos + 1);
}
})(curPos + 1);
return str.substring(startIndex, endIndex);
}
alert("Here's the word:" + getWord(str, pos));