Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.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 如果域中的字符与关键字匹配,则将其加粗_Javascript_Regex_Replace - Fatal编程技术网

Javascript 如果域中的字符与关键字匹配,则将其加粗

Javascript 如果域中的字符与关键字匹配,则将其加粗,javascript,regex,replace,Javascript,Regex,Replace,我需要在与关键字匹配的域部分加粗 // keyword: this|example // (pipe is separator) // domain: thisisanexample.com // result: <b>this</b>isan<b>example</b>.com //关键字:此|示例//(管道是分隔符) //域名:thisisanample.com //结果:thisisanple.com 这段代码的作用

我需要在与关键字匹配的域部分加粗

//    keyword: this|example // (pipe is separator)
//    domain:  thisisanexample.com
//    result:  <b>this</b>isan<b>example</b>.com
//关键字:此|示例//(管道是分隔符)
//域名:thisisanample.com
//结果:thisisanple.com
这段代码的作用是:

function colorDomain(domain, keyword) {
    var keywordSplit = keyword.split('|');
    var colorDomain = domain
    for (let index = 0; index < keywordSplit.length; index++) {

        if (keywordSplit[index].toLowerCase() != '') {
            var current = keywordSplit[index].toLowerCase();
            colorDomain = colorDomain.replace(current, "<b>" + keywordSplit[index].toLowerCase() + "</b>");
        }
    }
    return colorDomain;
}
函数颜色域(域,关键字){
var keywordSplit=关键字.split(“|”);
var colorDomain=域
for(让index=0;index
我遇到的问题是,它试图将以下数据加粗:

//    keyword:         say|yes
//    domain:          example-sayes.com
//    result:          example-<b>say</b>es.com
//    expected result: example-<b>sayes</b>.com
//关键字:说|是
//域名:example-sayes.com
//结果:example-sayes.com
//预期结果:example-sayes.com
如您所见,它只在域的第一部分加粗,因为“y”在“sayes”域中共享

请记住,关键字可能包含4+个条件,如下所示:

//    keyword: this|is|somthing|great // now there is shared 's' and shared 'g' and 't'

//    and for a domain like: greathisisomethinghello.com
//    it will produce:       grea<b>th<b>is</b></b>isomethinghello.com
//    expected result:       <b>greathisisomething</b>hello.com
//关键字:这|是|很好|//现在有了共享的's'和共享的'g'和't'
//对于像GreathisisSomethinghello.com这样的域名
//它将产生:greathisisomethinghello.com
//预期结果:greathisisomethinghello.com
然后它变得更复杂

我如何才能成功地加粗字符串考虑到这种情况,正则表达式模式可以解决这个问题?

var string=“www.google.de”;
var search=“gle”;
var begin=string.indexOf(搜索);
var end=begin+search.length;
var result=string.slice(0,begin)+++search++string.slice(end);
document.getElementById(“box”).innerHTML=结果

您可以创建一个数组,其中包含关键字出现在输入中的索引,合并相交部分,然后生成输出。以下是一个例子:

const keywords='this | is | something | great';
常量输入='greathisisomethinghello.com';
常量索引=关键字。拆分(“|”)。减少((a,c)=>{
设startIndex=input.indexOf(c);
while(startIndex!=-1){
a、 推({
startIndex,
endIndex:startIndex+c.length
});
startIndex=input.indexOf(c,startIndex+1);
}
返回a;
}, []);
sort({startIndex:a},{startIndex:b})=>a-b);
常数相交=(o1,o2)=>o1.startIndex{
a=`a.substring(0,c.startIndex)}
${a.substring(c.startIndex,c.endIndex)}
${a.substring(c.endIndex)}; 返回a; },输入);
控制台日志(输出)我的方法是首先生成一个“位图”,它是一个数组,每个普通字符有0个,每个粗体字符有1个,然后在位图中的0和1之间插入标记:

函数hilite(str,关键字){
//创建位图,0=普通字符,1=粗体字符
让位=[…str].fill(0);
关键词.split(“|”).forEach(kw=>{
设i=-1;
而(1){
i=str.indexOf(千瓦,i+1);
if(i<0)
打破
填充位(1,i,i+kw.长度);
}
});
//在位图中插入介于0/1和1/0之间的标记
让res='',
prev=0;
bits.forEach((b,i)=>{
if(b&!prev){
res+='';
}
如果(!b&&prev){
res+='';
}
res+=str[i];
prev=b;
});
如果(上一个)
res+=''
返回res;
}
log(hilite('thisisanexample.com','this | example'))
log(hilite('example-sayes.com','say | yes'))

console.log(hilite('greathisisomethinghello.com','this | is | something | great'))
示例sayes.com的预期结果是什么
?查看我的编辑和预期结果谢谢,非常有创意的解决方案!