Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript字符串word wrap对另一个字符串中的匹配项进行换行_Javascript_Jquery_String - Fatal编程技术网

javascript字符串word wrap对另一个字符串中的匹配项进行换行

javascript字符串word wrap对另一个字符串中的匹配项进行换行,javascript,jquery,string,Javascript,Jquery,String,如果我有一个特定的字符串输入,我希望与另一个字符串进行比较,并使用可能的最大匹配项将输入字符串的匹配项包装到另一个字符串中。我怎样才能最好地将这些火柴包装在标签中?这是一个非常重要的问题 基本上,我希望将输入的字符串与另一个字符串进行匹配,使用span标记显示在输入字符串中找到的目标的匹配部分 首先从输入字符串开始匹配(最大可能匹配) 应突出显示搜索词的部分匹配(参见示例中的“驳船”、“驳船”) 特殊字符分隔符应与“fred/dred”匹配。输入的字符应为两个单词 输入字符串将根据用户输入的内

如果我有一个特定的字符串输入,我希望与另一个字符串进行比较,并使用可能的最大匹配项将输入字符串的匹配项包装到另一个字符串中。我怎样才能最好地将这些火柴包装在标签中?这是一个非常重要的问题

基本上,我希望将输入的字符串与另一个字符串进行匹配,使用span标记显示在输入字符串中找到的目标的匹配部分

  • 首先从输入字符串开始匹配(最大可能匹配)
  • 应突出显示搜索词的部分匹配(参见示例中的“驳船”、“驳船”)
  • 特殊字符分隔符应与“fred/dred”匹配。输入的字符应为两个单词
  • 输入字符串将根据用户输入的内容而变化
  • 将输入字符串从开始处作为优先级进行匹配
  • 匹配出现的每个单词

如果用户输入一个包含多个单词的字符串,我希望从它们出现在第二个字符串中的开始,以增量方式包装它们的匹配项。在输入的字符串的开头/结尾可能有空格,也可能没有空格。我想把最大的部分包起来

输入字符串示例:

"Brown cats cannot be white cats"
"blue pigs "
"large, charged/marged barge pigs"
我想把它们包装成这样:

"<span class='wrapper'>Brown cats cannot be white cats</span>"
每个示例输入的最终字符串:

"Hi bill, <span class='wrapper'>brown cats cannot be white cats</span> and cows are not blue pigs, blue melons are large but not batteries charged barges with <span class='wrapper'>white cats</span> carry coal"

"Hi bill, brown cats cannot be white cats and cows are not <span class='wrapper'>blue pigs</span>, blue melons are large but not batteries charged barges with white cats carry coal"

"Hi bill, brown cats cannot be white cats and cows are not blue <span class='wrapper'>pigs</span>, blue melons are large but not batteries <span class='wrapper'>charged</span> <span class='wrapper'>barge</span>s with white cats carry coal"
如果我只是简单地包装每个匹配的单词,我可以做到:

function replaceWords(wordsy, text) {
   var re = '(' + wordsy + ')(?![^<]*(?:<\/script|>))',
       regExp = new RegExp(re, 'ig'),
       sTag = "<span class='wrapper'>",
       eTag = "</span>";
   return text.replace(regExp, sTag + '$&' + eTag);
};
var matchstring = "Brown cats cannot be white cats";
var wrapstring = "Hi bill, brown cats cannot be white cats and cows are not blue pigs, blue melons are large but not batteries charged barges with white cats carry coal";
var words = myValue.split(" ");
var i = words.length; while (i--) {
    wrapstring = replaceWords(words[i], wrapstring );
};
函数替换词(wordsy,text){
var re='('+wordsy+')(?![^这是怎么回事:(只描述算法,而不是用代码编写的)

想象你把两个字符串写在两张纸上。将两张纸放在一起,使其中一张在另一张上面。将上一张移到左边,使其最后一个字母位于下一张纸的第一个字母之上。现在,两个重叠的字母是否匹配?如果匹配,则长度为1。将其记录为最长的匹配。下一张n、 将上一页向右移动一个字符。现在两个字母重叠。它们匹配吗?如果匹配,则最大匹配大小为2。将上一页向右移动一个字符,每次都找到匹配的重叠字符的最大部分。始终跟踪最大匹配。继续移动,直到上一页结束为止在右侧,其第一个字符与另一张图纸的最后一个字符重叠

我不知道这在javascript中实现有多容易,但作为一种算法,我认为这是合理的

PS-对于需要查找“匹配的重叠字符的最大部分”的位,可以执行以下操作:

/* Note: str1 and str2 are the two overlapping portions of the strings */
var largestMatch = 0;
var currMatch = 0;
for (var i = 0; i < str1.length; i++) {
    if (str1[i] == str2[i]) currMatch++;
    else currMatch = 0;
    largestMatch = Math.max(largestMatch, currMatch);
}
// largestMatch is the size of the largest section of matched characters
/*注意:str1和str2是字符串的两个重叠部分*/
var-largestMatch=0;
var currMatch=0;
对于(变量i=0;i
以下是我为解决这一问题所做的:(寻找改进,因为它并不完美) (这被包装在jQuery文档中) 如下:

函数findStringLimit(searchChar、searchCharIndex、searchedString){
返回searchedString.substring(0,searchedString.lastIndexOf(searchChar,searchCharIndex));
};
函数替换词(wordsy,text){

var re='(“+wordsy+”)(?![^我有一个有趣的解决方案,它应该与您的原始规范一样工作。它还没有经过压力测试,我不确定您是否要处理大量文本,并且它进行了很多正则表达式匹配。不一定是最干净或最简单的解决方案,但它可以达到预期的效果

功能和限制:

  • 它处理匹配字符串中的大多数奇怪情况,例如重复的单词、非常相似或重复的短语等

  • 目前,源字符串中不能可靠地包含
    [
    ]
    字符,因为它们是内部使用的。如果这是一个问题,您必须在匹配之前将它们替换为任何其他字符或字符组合

  • 对于包含
    N
    单词的匹配字符串,
    2*N+5
    字符串替换是使用复杂度不同的正则表达式完成的

  • 它匹配不区分大小写的单词和短语,忽略任何非单词字符。同时,它在结果中保留大小写混合的单词和非单词字符

工作原理:

  • 首先,它分别查找每个单词,并将匹配字符串中的索引附加到方括号中:
    word[2]
    。如果该单词出现多次,它将附加所有索引:
    word[3][2][1]

  • 接下来,它通过查看周围单词的索引来查找和标记不在包装边界上的单词。在另一个步骤中,它从这些单词中删除索引。最后,
    one[1]two[2]three[3]
    将变成
    one[1][]two-three[3]

  • 现在剩下的就是按照一定的顺序做一些假设,并包装单词/短语。看看代码,看看所有的替换都完成了

  • 重要的是,在第一步之后,我们从不直接匹配单词,从那时起,单词只被称为[index]
    之前的
    任意数量的单词字符或[]
    之后的
    任意数量的单词字符。这确保了我们正确地包装重复的单词/短语

    看一看。我添加了一个悬停效果,所以你可以看到哪些单词被分组和包装在一起

    这是疯狂的代码,享受吧

    var matchstring = 'Brown cats cannot be white cats';
    var wrapstring = 'Hi bill, brown cats cannot be white cats and cows are not blue pigs, blue melons are large but not batteries charged barges with white cats carry coal, and the word "cannot" should match ';
    
    // Pre-process matchstring to make it a flat list of words
    // separated by single spaces.
    matchstring = matchstring.replace(/\W+/g,' ');
    
    var wrapStart = '<span class="wrapped">';
    var wrapEnd = '</span>';
    
    var matcharray = matchstring.split(' ');
    var i, reg;
    
    // Mark all matched words with indices
    // one -> one[1]
    for (i = 0; i < matcharray.length; i++) {
        reg = new RegExp('\\b' + matcharray[i] + '\\b', 'ig');
        wrapstring = wrapstring.replace(reg, '$&[' + i + ']');
    }
    
    // Mark all inner words
    // one[1] two[2] three[3] -> one[1] []two[2] three[3]
    for (i = 1; i < matcharray.length; i++) {
        reg = new RegExp('\\b(\\w+)([\\]\\d\\[]*\\[' + (i - 1) + '\\][\\]\\d\\[]*)(\\W+)(\\w+)([\\]\\d\\[]*\\[' + i + '\\][\\]\\d\\[]*)(?=\\W+\\w+[\\[\\d\\]]*\\[' + (i + 1) + '\\])', 'ig');
        wrapstring = wrapstring.replace(reg, '$1$2$3[]$4$5');
    }
    
    // Remove indices from inner words
    // one[1] []two[2] three[3] -> one[1] []two three[3]
    wrapstring = wrapstring.replace(/\[\](\w+)[\[\d\]]*/g, '[]$1');
    
    // Start tags
    // one[1] []two three[3] -> {one []two three[3]
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\](\W+)\[\]/g, wrapStart + '$1$2[]');
    
    // End tags
    // {one []two three[3] -> {one []two three}
    wrapstring = wrapstring.replace(/\[\](\w+\W+\w+)\[[\[\d\]]+\]/g, '$1' + wrapEnd);
    
    // Wrap double words
    // one[1] two[2] -> {one two}
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\](\W+\w+)\[[\[\d\]]*\]/g, wrapStart + '$1$2' + wrapEnd);
    
    // Orphan words
    // unmatched matched[1] unmatched -> unmatched {matched} unmatched
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\]/g, wrapStart + '$1' + wrapEnd);
    
    // Remove left-over tags
    // []word -> word
    wrapstring = wrapstring.replace(/\[\]/g, '');
    
    alert(wrapstring);
    
    正则表达式中的
    \b
    表示匹配单词边界,即一系列单词字符的开头或结尾。这就是为什么上面的
    \b
    正则表达式只给出整个单词,因为
    单词
    需要被单词边界包围。但不一定要这样

    如果我们想匹配文本中以
    /* Note: str1 and str2 are the two overlapping portions of the strings */
    var largestMatch = 0;
    var currMatch = 0;
    for (var i = 0; i < str1.length; i++) {
        if (str1[i] == str2[i]) currMatch++;
        else currMatch = 0;
        largestMatch = Math.max(largestMatch, currMatch);
    }
    // largestMatch is the size of the largest section of matched characters
    
    function findStringLimit(searchChar, searchCharIndex, searchedString) {
        return searchedString.substring(0, searchedString.lastIndexOf(searchChar, searchCharIndex));
    };
    
    function replaceWords(wordsy, text) {
        var re = '(' + wordsy + ')(?![^<]*(?:<\/script|>))',
            regExp = new RegExp(re, 'ig'),
            sTag = "<span class='wrappedWord'>",
            eTag = "</span>";
        return text.replace(regExp, sTag + '$&' + eTag);
    
    };
    var longstring = $('#mystring');
    var htmlString =longstring .html(); //  instance html
    myValue = "Brown cats cannot be white cats";
    myValue = myValue.replace(/^\s+|\s+$/g, "");//trim whitespace at each end
    
    var words = myValue.split(" ");
    var allPhrases = [];
    allPhrases.push(myValue);
    
    var i = words.length;
    while (i--) {
        allPhrases.push(findStringLimit(" ", allPhrases[(words.length - i) - 1].length, allPhrases[(words.length - i) - 1]));
    };
    
    var i = allPhrases.length;
    while (i--) {
        if (allPhrases[i] != "") words = words.concat(allPhrases[i]);
    };
    var i = words.length;
    while (i--) {
        htmlString = replaceWords(words[i], htmlString);
    };
    longstring.html(htmlString);
    
    var matchstring = 'Brown cats cannot be white cats';
    var wrapstring = 'Hi bill, brown cats cannot be white cats and cows are not blue pigs, blue melons are large but not batteries charged barges with white cats carry coal, and the word "cannot" should match ';
    
    // Pre-process matchstring to make it a flat list of words
    // separated by single spaces.
    matchstring = matchstring.replace(/\W+/g,' ');
    
    var wrapStart = '<span class="wrapped">';
    var wrapEnd = '</span>';
    
    var matcharray = matchstring.split(' ');
    var i, reg;
    
    // Mark all matched words with indices
    // one -> one[1]
    for (i = 0; i < matcharray.length; i++) {
        reg = new RegExp('\\b' + matcharray[i] + '\\b', 'ig');
        wrapstring = wrapstring.replace(reg, '$&[' + i + ']');
    }
    
    // Mark all inner words
    // one[1] two[2] three[3] -> one[1] []two[2] three[3]
    for (i = 1; i < matcharray.length; i++) {
        reg = new RegExp('\\b(\\w+)([\\]\\d\\[]*\\[' + (i - 1) + '\\][\\]\\d\\[]*)(\\W+)(\\w+)([\\]\\d\\[]*\\[' + i + '\\][\\]\\d\\[]*)(?=\\W+\\w+[\\[\\d\\]]*\\[' + (i + 1) + '\\])', 'ig');
        wrapstring = wrapstring.replace(reg, '$1$2$3[]$4$5');
    }
    
    // Remove indices from inner words
    // one[1] []two[2] three[3] -> one[1] []two three[3]
    wrapstring = wrapstring.replace(/\[\](\w+)[\[\d\]]*/g, '[]$1');
    
    // Start tags
    // one[1] []two three[3] -> {one []two three[3]
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\](\W+)\[\]/g, wrapStart + '$1$2[]');
    
    // End tags
    // {one []two three[3] -> {one []two three}
    wrapstring = wrapstring.replace(/\[\](\w+\W+\w+)\[[\[\d\]]+\]/g, '$1' + wrapEnd);
    
    // Wrap double words
    // one[1] two[2] -> {one two}
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\](\W+\w+)\[[\[\d\]]*\]/g, wrapStart + '$1$2' + wrapEnd);
    
    // Orphan words
    // unmatched matched[1] unmatched -> unmatched {matched} unmatched
    wrapstring = wrapstring.replace(/(\w+)\[[\[\d\]]+\]/g, wrapStart + '$1' + wrapEnd);
    
    // Remove left-over tags
    // []word -> word
    wrapstring = wrapstring.replace(/\[\]/g, '');
    
    alert(wrapstring);
    
    reg = new RegExp('\\b' + matcharray[i] + '\\b', 'ig');
    
    reg = new RegExp('\\b' + matcharray[i] + '\\w*\\b', 'ig');