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

时间:2017-10-20 10:24:07

标签: javascript jquery

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

  

他有Ashish Nehra,他将退出各种形式的比赛   不久,从Pathan那里获取灵感,“他一直是个人   精彩的战士。他在这方面做得很好   年龄和表明任何人都可以做到。我不是那么老,我在工作   很难,我的健康现在非常好。“”我想改善我的守备   但我不是在想自己。“

我需要在“战士”之后和之前获得50个字符。

4 个答案:

答案 0 :(得分:3)

获取您要查找的单词的索引并考虑其长度,然后获取下一个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);

答案 1 :(得分:2)

我可以给你一个javascript解决方案。首先通过匹配“战士”将大字符串分成两个。之后使用substring来检索特定字符串之前和之后。 希望只有一个“战士”。

    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." ;

    array = str.split("warrior") ;
    console.log( array[0].substring(array[0].length - 50 ) ) ;
    console.log( array[1].substring(0,50) ) ;

答案 2 :(得分:0)

您正在寻找substring来获取字符串的一部分。

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

答案 3 :(得分:0)

您可以使用split()和slice()。

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);

希望有所帮助!