gpt4 book ai didi

javascript - 正则表达式捕获组到大写

转载 作者:行者123 更新时间:2023-11-30 15:58:48 24 4
gpt4 key购买 nike

如果我使用此代码将单词设为粗体,我该如何将它们也设为大写?

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);

最佳答案

您可以简单地提供回调来执行复杂的操作:

function makeBoldAndUpper(input, wordsToBold) {
return input.replace(
new RegExp('\\b(' + wordsToBold.join('|') + ') \\b','ig'),
function(match, capture) { return "<b>"+match.toUpperCase()+"</b>"; });
}

关于javascript - 正则表达式捕获组到大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com