简体   繁体   English

在编写代码以搜索网页中的短语时需要帮助

[英]Need help in writing code to search phrase in a Webpage

I am developing a js file that will take the search string from the Clipboard and will highlight the phrase in the Web page if that is found. 我正在开发一个js文件,该文件将从剪贴板中获取搜索字符串,并将在网页中突出显示该短语。
Here I will copy paste the text from ms word. 在这里,我将从ms word复制粘贴文本。 And I am using below regular expressions to clean word formatting from clipboard text.. 我正在使用以下正则表达式清除剪贴板文本中的单词格式。

   var pure = impure.replace(/“/g, "\"");
   pure = pure.replace(/”/g, "\"");
   pure = pure.replace(/’/g, "\'");
   pure = pure.replace(/‘/g, "\'");
   pure = pure.replace(/–/g, "\-");

and then searching the phrase with the following method 然后使用以下方法搜索词组

   var body = $('body');
   var replaced = body.html().replace(''+pure+'','<span class="highlight">'+pure+'</span>');
   $("body").html(replaced);

With this I am able to find individual words and some small sentences.. But i am unable to find the total phrase at a time... For example look at this fiddle(This works in only IE, cause i am currently targeting IE). 这样我就可以找到单个单词和一些小句子。。但是我一次无法找到整个短语……例如看这个小提琴(仅在IE中有效,因为我当前针对的是IE) 。

Please suggest me a good way to proceed further.. Here my Word may contains Bullets, Newlines, Bolds, Italics etc... 请给我建议一个进一步的好方法。.我的话语可能包含项目符号,换行符,粗体,斜体等。

I'm not sure what was the exact bug you were having doubts about, but I found some bugs of my own in your script. 我不知道什么是你有心存疑虑确切的错误,但我发现我自己的一些错误的脚本。 I fixed them here: http://jsfiddle.net/F7txy/6/ 我在这里固定了它们: http : //jsfiddle.net/F7txy/6/

One of the problems is that you appeared to be trying to replace the pure text with pure again: 问题之一是您似乎试图再次用pure替换pure文本:

var replaced = body.text().replace(
    '' + pure + '',
    '<div class="highlight">' + pure + '</div>'
);

It looks like you should have been replacing the impure with pure: 看起来您应该已经用pure代替了impure

var replaced = body.text().replace(
    '' + impure + '',
    '<div class="highlight">' + pure + '</div>'
);

Also, you can chain the replace methods together (which I did for you). 另外,您可以将replace方法链接在一起(这是我为您完成的)。 Also, you could combine all of them into one but I'll let you do that. 另外,您可以将所有这些合并为一个,但我会让您做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM