Javascript 如何从文本中提取一个单词并将其转换为链接?

Javascript 如何从文本中提取一个单词并将其转换为链接?,javascript,hyperlink,Javascript,Hyperlink,我只希望大写字母是一个超链接,这似乎真的很容易,但我找不到一个在线答案。-- 像这样的东西就行了 <script> function myClick(){ text = "<b><i>this is my text, THIS NEEDS TO BE A HYPERLINK </i></b>"; document.getElementById('heading1').innerHTML = document.getElementByI

我只希望大写字母是一个超链接,这似乎真的很容易,但我找不到一个在线答案。--

像这样的东西就行了

<script>
function myClick(){
text = "<b><i>this is my text, THIS NEEDS TO BE A HYPERLINK </i></b>";
document.getElementById('heading1').innerHTML  = document.getElementById('heading1').innerHTML + text;
document.getElementById("heading1").style.textAlign="left";
document.getElementById("heading1").style.marginLeft="5px";

}
</script>

文本是否总是大写?你是自己写文本,只需要自动处理还是解析一些html?我不是一个javascript高手,所以我想合并一些html使其成为超链接。顺便说一下,这不是实际的文本,这只是一个例子。在实际文本中,我只希望有一个单词是超链接。你会将它链接到什么?我正在帮助您提供答案。您需要使用regex和.replace
var text = "<b><i>this is my text, THIS NEEDS TO BE A HYPERLINK </i></b>";
// Regex matching only uppercase letters and whitespaces where there
// have to be at least 2 in a row to match
var match = text.match(/[A-Z\s]{2,}/);
if (match && match.length) {
  match.forEach(function (str) {
    text = text.replace(str, '<a href="http://google.com/q=regex">' + str + '</a>');
  });
}
    var match = text.match(/[A-Z]{2,}/);