Javascript 使用jquery在特定文本前后抓取50个字符

Javascript 使用jquery在特定文本前后抓取50个字符,javascript,jquery,Javascript,Jquery,我需要使用jquery在特定文本前后获得50个字符。 例如: 他有阿什·尼赫拉,他将退出所有形式的比赛 很快,帕桑就从中获得了灵感,他说:“他是一个 伟大的勇士。他在这场比赛中复出,做得很好 我没有那么老,我正在工作 “我很努力,现在我的身体状况很好。”“我想提高我的防守能力 但我没有超前思考。” 我需要在单词warrior前后各获取50个字符。您要获取字符串的一部分 var str = "Hello world!"; var res = str.substring(0, 4); echo re

我需要使用jquery在特定文本前后获得50个字符。 例如:

他有阿什·尼赫拉,他将退出所有形式的比赛 很快,帕桑就从中获得了灵感,他说:“他是一个 伟大的勇士。他在这场比赛中复出,做得很好 我没有那么老,我正在工作 “我很努力,现在我的身体状况很好。”“我想提高我的防守能力 但我没有超前思考。”

我需要在单词warrior前后各获取50个字符。

您要获取字符串的一部分

var str = "Hello world!";
var res = str.substring(0, 4);
echo res;
// Result: Hell

我可以给你一个javascript解决方案。首先,通过匹配勇士将大绳一分为二。之后,使用子字符串检索特定字符串之前和之后的内容。 希望只有一个战士

var str=他有阿什什·尼赫拉(Ashish Nehra),他将很快从各种形式的比赛中退役,从中获得灵感。帕桑说:“他是一名出色的战士。他在这个年龄复出的表现非常出色,证明了任何人都能做到。我没有那么老,我正在努力工作,现在我的身体状况非常好。”“我想提高我的防守能力,但我没有超前考虑。”; 数组=str.splitwarrior; console.log数组[0]。子字符串数组[0]。长度-50;
console.log数组[1]。子字符串0,50;获取要查找的单词的索引并考虑其长度,然后抓取接下来的50个字符和之前的50个字符,如有必要,将它们连接起来:

const input = "He has Ashish Nehra, who will retire from all forms of the game shortly, to take inspiration from and Pathan says, “he has been a wonderful warrior. He has done very well to make a comeback at this age and shows anyone can do it. I am not that old and I am working hard and my fitness is very good now.” “I want to improve my fielding but I am not thinking ahead of myself.”"

let index = input.indexOf("warrior"),
    offset = "warrior".length;

let charactersAfter = input.substr(index + offset, 50),
    charactersBefore = input.substr(0, index).slice(-50),
    joined= charactersAfter + charactersBefore;

console.log(charactersAfter);
console.log(charactersBefore);
console.log(joined);
如果该词重复出现,则预期会出现意外结果或澄清要求

注意:没有使用jQuery,因为它根本不是必需的

编辑:

使用正则表达式匹配整个单词:

const input = "He has Ashish Nehra, who will retire from all forms of the game shortly, to take inspiration from and Pathan says, “he has been a wonderful warrior. He has done very well to make a comeback at this age and shows anyone can do it. I am not that old and I am working hard and my fitness is very good now.” “I want to improve my fielding but I am not thinking ahead of myself.”"

let index = input.match(/\bwarrior\b/).index,
    offset = "warrior".length;

let charactersAfter = input.substr(index + offset, 50),
    charactersBefore = input.substr(0, index).slice(-50),
    joined= charactersAfter + charactersBefore;

console.log(charactersAfter);
console.log(charactersBefore);
console.log(joined);

可以使用“拆分”和“切片”

var str='He has Ashish Nehra, who will retire from all forms of the game shortly, to take inspiration from and Pathan says, “he has been a wonderful warrior. He has done very well to make a comeback at this age and shows anyone can do it. I am not that old and I am working hard and my fitness is very good now.” “I want to improve my fielding but I am not thinking ahead of myself.”';
var keyword = 'warrior';
var fifty_ch_before_keyword = str.split(keyword)[0].slice(-50);
var fifty_ch_after_keyword = str.split(keyword)[1].slice(0, 50);

希望有帮助!

OP要求在单词warrior前后输入字符串。他只需要单词warrior前后的50个字符。可能有超过1个单词出现的情况。我认为@Tomasz您的答案不符合他的要求。嗨,我在这里发现了一个问题,如果文本是这样的,他有Ashish Nehra,他将重新输入帕桑说:“他是一名出色的勇士。他在这个年龄复出的表现非常出色,证明了任何人都能做到。我没有那么老,我正在努力工作,现在我的身体状况非常好。”“我想提高我的防守能力,但我没有超前考虑。”我想,这不会正常工作。@Raru你能更具体地说明什么不起作用吗?请看文本。我已经将文本中的单词warrior改为warriors。我正在尝试检查单词warrior。我仍然不理解这个问题。字符串warrior存在,即使附加了字符,问题也不存在将其视为一项要求。@GorkaHernandez我同意jQuery不是必需的,但它肯定可以减少这种情况下所需的代码量。+1,回答得好,但是避免在示例中使用警报,而是使用console.log。这将在一个位置显示所有文本输出,通常比关闭多个弹出窗口更令人不快。