简体   繁体   English

Javascript:使用变元音,变音符号搜索和splitText,

[英]Javascript: search and splitText with mutated vowel, umlauts,

i'm using an free Javascript to search and highlight text on a website. 我正在使用免费的Javascript搜索和突出显示网站上的文本。 If the search-stirng is part of an word, the hole word should be highlighted. 如果搜索搅拌是单词的一部分,则应突出显示空洞单词。 Example: search for "over", than "stackoverflow" have to be highlighted. 示例:搜索“ over”,而不是“ stackoverflow”必须突出显示。

So the first step is to search the text using regular expressions to select the beginning of the whole word, and return the start-position: 因此,第一步是使用正则表达式搜索文本以选择整个单词的开头,然后返回起始位置:

var string='over'; //String to search
var pattern= new RegExp("[a-zA-Záéíóäëiöúàèììùüß-]*"+string,'i');
var position = node.data.toUpperCase().search(pattern);

Next is to call the highlight function.... 接下来是调用高亮功能。

            if (position >= 0)
                return highlight(node, position, string);
            else
                return 0;

... which used the start position and detects the last character (endbit) of an word ...使用起始位置并检测单词的最后一个字符(结束位)

function highlight(node, position, string){
                var middlebit = node.splitText(position);
                var endbit = middlebit.splitText(middlebit.data.search(/[^a-zA-Záéíóäëiöúàèììùüß-]/));
[...] next: highlight the word...

Thats working fine, as long as you NOT use mutated vowel, umlauts and so on. 只要您不使用变异的元音,变音符号等,就可以正常工作。 For example: if you search for over, the whole word "stackoverflow" is highlighted. 例如:如果搜索结束,整个单词“ stackoverflow”将突出显示。 If u search for "würstchen" (for example in bratwürstchen) it doesn't work. 如果您搜索“würstchen”(例如在bratwürstchen中),则该搜索无效。 It lookes like, that 看起来像那样

 var position = node.data.toUpperCase().search(pattern);

or 要么

var middlebit = node.splitText(position);

Does not work with that kind of special characters. 不适用于这种特殊字符。

Hopefully anybody has an idea how to slove that problem. 希望任何人都有解决这个问题的想法。

Thank you so much, 非常感谢,

Bndr 本德尔

In order to answer my own question: After 2 days of "try-and-error" and finally writing this question, I find an missing "i" at this line: 为了回答我自己的问题:经过2天的“尝试和错误”并最终写下了这个问题,我在此行发现缺失的“ i”:

var endbit = middlebit.splitText(middlebit.data.search(/[^a-zA-Záéíóäëiöúàèììùüß-]/));

there is an wrong expression to find the endbit. 查找终止位的表达式有误。 so the right one is: 所以正确的是:

var endbit = middlebit.splitText(middlebit.data.search(/[^a-zA-Záéíóäëiöúàèììùüß-]/i));

small cause, big impact ;-) 小原因,大影响;-)

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

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