我有一个包含多个句子的字符串。我也有当前的光标/插入位置。
我需要能够在给定的光标位置提取当前句子。
例如,取这个字符串:
This is the first sentence. And this is the second! Finally, this is the third sentence
如果当前光标位置为33
,则光标位于第二个句子中。
在这种情况下,返回的结果应为:
And this is the second!
我只需要使用.?!
对此的任何帮助将不胜感激。
虽然我期望需要正则表达式,但如果使用本机方法有更快的替代方法,我也会对此感兴趣。
答案 0 :(得分:1)
以下是实现所需内容的方法:使用String#split
和/[?!.]/g
来获取句子数组,然后遍历数组以总结所找到句子的长度,如果index小于计数,返回句子。
function getSentenceByPos(idx, str) {
pos = 0;
array = str.split(/[?!.]/g);
for (var i=0; i<array.length; i++) {
pos += array[i].length + 1;
if (pos >= idx) {
return array[i];
}
}
}// 26 still 1 then `.`. 51 then `!` - 53 is 3rd sentence!
document.write(getSentenceByPos(53, "This is the first sentence. And this is the second! Finally, this is the third sentence"));
&#13;
答案 1 :(得分:1)
我想添加一个不使用正则表达式来分割的答案 字符串,因为这样做是非常低效的,可能会很慢 更大的文本块。
最有效的方法可能是使用几个循环进行搜索,只需要2次传递即可找到句子的末尾。
var sentenceFromPos = function (s, pos) {
var len = s.length,
start,
end,
char;
start = pos;
end = pos;
while (start >= 0) {
char = s.charAt(start);
if (char === '.' || char === '?' || char === '!') {
break;
}
start -= 1;
}
while (end < len) {
char = s.charAt(end);
if (char === '.' || char === '?' || char === '!') {
break;
}
end += 1;
}
return s.substring(start + 1, end + 1).trim();
};
var phrase = 'This is the first sentence. And this is the second! Finally, this is the third sentence';
console.log(sentenceFromPos(phrase, 10));
console.log(sentenceFromPos(phrase, 33));
console.log(sentenceFromPos(phrase, 53));
答案 2 :(得分:0)
此功能会尊重游标超过短语的限制(例如!
或.
)
function getPhrase(string, cursor) {
phrases = string.match(/.*?(!|\.|$)/g)
basecursor = 0;
phrase = phrases[0]
for(ii=0; ii<phrases.length-1; ii++) {
if (basecursor+phrases[ii].length<cursor) {
phrase = phrases[ii+1]
basecursor += phrases[ii].length
}
}
return(phrase)
}
string = "This is the first sentence. And this is the second! Finally, this is the third sentence"
cursor = 0
phrase = getPhrase(string, cursor)
document.write(phrase)