如何从regexp replace中排除jpeg-names?

时间:2016-12-09 09:35:31

标签: javascript regex documentation

我使用搜索功能作为文档网站,在选择搜索匹配时会显示突出显示文本的页面(就像pdf阅读器或netbeans一样)。

要实现突出显示我使用javascript:

function searchHighlight(searchTxt) {
  var target = $('#page').html();
  var re = new RegExp(searchTxt, 'gi');
  target = target.replace(
    re,
    '<span class="high">' + searchTxt + '</span>'
  );
  $('#page').html(target);
}

问题/问题:

由于页面包含基于md5的文件名图像,因此一些搜索会使图像src混乱。

搜索&#34; 1000&#34;会歪曲

<img src="53451000abababababa---.jpg"

<img src="5334<span class="hl">1000</span>abababab--.jpg">

是否有可能用regexp来解决这个问题,不知何故将ajcent排除在&#34; .jpg&#34;?

还是可以在highligting用占位符替换图像之前,并在替换之后恢复为src?

示例:

  • 替换所有&lt; img *&gt;使用{{I-01}},{{I-02}}等,并将真正的src保存在var中。
  • 做上面的替换。
  • 从{{I-01}}恢复为&lt; img src =&#34; ..&#34; /&gt;

DOM操作当然是一种选择,但我认为这可以用regexp以某种方式完成,但是,我的正则表达式技能缺乏严重。

更新 这段代码现在适用于我:

function searchHighlight(searchTxt) {    
var stack = new Array();
var stackPtr = 0;
var target = $('#page').html();
//pre
target = target.replace(/<img.+?>/gi,function(match) {
  stack[stackPtr] = match;
  return '{{im' + (stackPtr++) + '}}';
});
//replace
var re = new RegExp(searchTxt, 'gi');
target = target.replace(re,'<span class="high">' + searchTxt + '</span>');
//post
stackPtr = 0;
target = target.replace(/{{im.+?}}/gi,function(match) {
    return stack[stackPtr++];
});
$('#page').html(target);
}

1 个答案:

答案 0 :(得分:1)

一种方法是创建一个包含所有可能有效搜索项的数组。在.textContent父元素中将<span>元素的#page设置为searchHighlight

searchTxt函数检查searchTxt是否与数组中的元素匹配。如果span与数组元素匹配,请使用匹配数组元素的索引选择"high"元素,在匹配的.className元素处切换#page span searchTxt,否则通知用户$(function() { var words = []; var input = $("input[type=text]"); var button = $("input[type=button][value=Search]"); var reset = $("input[type=button][value=Reset]"); var label = $("label"); var page = $("#page"); var contents = $("h1, p", page).contents() .filter(function() { return this.nodeType === 3 && /\w+/.test(this.nodeValue) }).map(function(i, text) { var span = text.nodeValue.split(/\s/).filter(Boolean) .map(function(word, index) { words.push(word); return "<span>" + word + "</span> " }); $(text.parentElement).find(text).replaceWith(span); }) var spans = $("span", page); button.on("click", function(event) { spans.removeClass("high"); label.html(""); if (input.val().length && /\w+/.test(input.val())) { var terms = input.val().match(/\w+/g); var indexes = $.map(terms, function(term) { var search = $.map(words, function(word, index) { return word.toLowerCase().indexOf(term.toLowerCase()) > -1 && index }).filter(Boolean); return search }); if (indexes.length) { $.each(indexes, function(_, index) { spans.eq(index).addClass("high") }) } else { label.html("Search term <em>" + input.val() + "</em> not found."); } } }); reset.on("click", function(event) { spans.removeClass("high"); input.val(""); label.html(""); }) })与任何有效的搜索字词都不匹配。

.high {
  background-color: #caf;
}
label em {
  font-weight: bold;
  background-color: darkorange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<input type="button" value="Search" />
<input type="button" value="Reset" />
<label></label>
<div id="page" style="max-width:500px;border:1px solid #ccc;">
  <h1 style="margin:0px;">test of replace</h1>
  <p>After Luke comes to Dagobah, Yoda initially withholds his true identity. He’s trying to get a sense of who Luke is as a person; Yoda understands that there’s a lot at risk in training Luke to be a Jedi, especially considering what happened with his
    father.
    <img style="float:right;" width="200" src="http://a.dilcdn.com/bl/wp-content/uploads/sites/6/2013/11/04-400x225.jpg">And Yoda is not impressed — Luke is impatient and selfish. With “Adventure. Excitement. A Jedi craves not these things,” the Jedi Master makes clear that Luke must understand the significance and meaning of the journey he thinks he wants to make.
    It’s an important lesson for Luke and for audiences, because when Luke faces Vader at the film’s climax, we see the stakes involved in the life of a Jedi</p>
  <p>Now Yoda-search works, however a search on "sites" will break the image-link. (Yes, I know this implementation isn't perfect but I'm dealing with reality)</p>
</div>
NSArray