Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 - Fatal编程技术网

Javascript 将正则表达式捕获组转换为大写

Javascript 将正则表达式捕获组转换为大写,javascript,regex,Javascript,Regex,如果我用这段代码把单词加粗,我该怎么把它们也变成大写呢 var vow = "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am th

如果我用这段代码把单词加粗,我该怎么把它们也变成大写呢

var vow = "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come.";

var wordsToBold=["night","watcher"];

function makeBold(input, wordsToBold) {
return input.replace(new RegExp('(\\b)(' + wordsToBold.join('|') + ')(\\b)','ig'), '$1<b>$2</b>$3');
}

document.getElementById("vow_p").innerHTML = makeBold(vow, wordsToBold);
var-vow=“夜幕降临,现在我的表开始了。直到我死了它才会结束。我不娶妻子,不占有土地,不生孩子。我不会戴王冠,也不会赢得荣誉。我将在我的岗位上生死攸关。我是黑暗中的剑。我是墙上的守望者。我是守护人类王国的盾牌。我向守夜人保证我的生命和荣誉,为了今夜和未来的所有夜晚。”;
var wordsToBold=[“夜晚”,“守望者”];
函数makeBold(输入,wordsToBold){
返回input.replace(新的RegExp('(\\b)('+wordsToBold.join('|'))+'(\\b)“,'ig'),'$1$2$3');
}
document.getElementById(“vow_p”).innerHTML=makeBold(vow,wordsToBold);

一种方法是使用CSS在
\vow\u p
元素中设置
元素的样式:

var vow=“夜幕降临,现在我的表开始了。直到我死了它才会结束。我不娶妻子,不占有土地,不生孩子。我不会戴王冠,也不会赢得荣誉。我将在我的岗位上生死攸关。我是黑暗中的剑。我是墙上的守望者。我是守护人类王国的盾牌。我向守夜人保证我的生命和荣誉,为了今夜和未来的所有夜晚。”;
var wordsToBold=[“夜晚”,“守望者”];
函数makeBold(输入,wordsToBold){
返回input.replace(新的RegExp('(\\b)('+wordsToBold.join('|'))+'(\\b)“,'ig'),'$1$2$3');
}
document.getElementById(“vow_p”).innerHTML=makeBold(vow,wordsToBold);
#誓言{
字体大小:粗体;
文本转换:大写;
}

您只需提供回调即可执行复杂操作:

function makeBoldAndUpper(input, wordsToBold) {
    return input.replace(
        new RegExp('\\b(' + wordsToBold.join('|') + ')  \\b','ig'),
        function(match, capture) { return "<b>"+match.toUpperCase()+"</b>"; });
}
函数makeBoldAndUpper(输入,wordsToBold){
返回输入。替换(
新的RegExp('\\b('+wordsToBold.join('|')+')\\b',ig'),
函数(match,capture){return”“+match.toUpperCase()+”“;});
}

通过将所有单词连接起来,您基本上可以构建一个正则表达式:

(\b)(night|watch)(\b)
此正则表达式将查找与任何请求粗体字匹配的所有有界字

我添加了一个映射函数来转换每个单词表达式,以支持复数和所有格名词

var vow=“夜幕降临,现在我的表开始了。直到我死了它才会结束。我不娶妻子,不占有土地,不生孩子。我不会戴王冠,也不会赢得荣誉。我将在我的岗位上生死攸关。我是黑暗中的剑。我是墙上的守望者。我是守护人类王国的盾牌。我向守夜人保证我的生命和荣誉,为了今夜和未来的所有夜晚。”;
document.body.innerHTML=makeBold(誓言,['night','watcher','bold');
函数makeBold(段落、单词、类名){
回油通道.更换(
新的RegExp('(\\b)('+words.map)函数(word){
返回单词+'[\'s]*';//捕获(大多数)复数和所有格名词。
}).join(“|”)+”(\\b)“ig”),
'$1$2$3');
}
.bold{
字体大小:粗体;
文本转换:大写;

}
我会在类中添加一个span:
'$1$2$3
…哦,我完全同意在这种情况下不应该使用
,我只是还没有写完答案:)