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

更改动态文本,该文本为';用星号(*)封装到使用JavaScript的网页链接的

更改动态文本,该文本为';用星号(*)封装到使用JavaScript的网页链接的,javascript,html,jquery,hyperlink,Javascript,Html,Jquery,Hyperlink,我有一个奇怪的情况,我需要更改html页面上的文本,如下所示: *www.google.co.uk/Google* *www.stackoverflow.com/StackOverflow* var html=document.getElementsByTagName('body')[0].innerHTML; html = html.replace(/\*([^*]+)\*/g , '<a href="www.google.co.uk>placeholder</a>"

我有一个奇怪的情况,我需要更改html页面上的文本,如下所示:

*www.google.co.uk/Google*
*www.stackoverflow.com/StackOverflow*
var html=document.getElementsByTagName('body')[0].innerHTML;
html = html.replace(/\*([^*]+)\*/g , '<a href="www.google.co.uk>placeholder</a>"');
document.getElementsByTagName('body')[0].innerHTML=html;
到锚定标记:

<a href="www.google.co.uk/">Google</a>
<a href="www.stackoverflow.com/">StackOverflow</a>
结果是打印出了链接,但我想我会得到一些帮助

提前谢谢

编辑:*2

网站上的html看起来像:

<div class="tnc"> By making a booking, You are agreeing to the  *https://digital.nhs.uk/about-nhs-digital/terms-and-conditions^terms and conditions* and will follow the COVID-19 precautions set in the email confirmation </div> 

好的-这是您需要的解决方案。我做了一些命名,使它更具可读性。它的作用如下:

  • 它在每个
    中搜索与正则表达式模式
    /\*(?.*[.].*)\^(?.*\*/
  • 这将查找
    *
    ,然后开始以“域”的名称进行捕获

  • 它搜索分隔符
    ^
    ,在该位置停止第一个捕获组,并启动名为“text”的下一个捕获组

  • 它会一直捕获,直到到达下一个
    *

  • 它运行replace命令,删除整个正则表达式匹配(从
    *
    *
    ),并创建一个链接
    `);
    });
    
    a{
    显示:内联块;
    }
    预订即表示您同意*https://digital.nhs.uk/about-nhs-digital/terms-and-conditions^条款和条件*,并将遵循电子邮件确认中设定的新冠病毒-19预防措施
    通过预订,您就同意了*https://google.com/about-nhs-digital/terms-and-conditions^条款和条件*,并将遵循电子邮件确认中设定的新冠病毒-19预防措施
    通过预订,您就同意了*https://yetanotherdomain.com/about-nhs-digital/^另一个字符串*将遵循电子邮件确认中设置的新冠病毒-19预防措施
    
    通过预订,您就同意了*https://digital3.nhs.uk/about-nhs-digital/terms-and-conditions^其他条款*和将遵循电子邮件确认中设置的新冠病毒-19预防措施
    是否分开?或者他们只是和新词在同一个部门?好吧,我想我明白你的意思。你可以发布包含你正在寻找的链接的HTML吗?我可以找到一种方法来抓取它并获取这些对象的数组吗?你必须编辑你的OP以包含HTML,因为你不能在评论中发布多行内容。:)是的,看,这更难处理。该示例中有多个分隔符
    /
    ,因此它只能捕获根级别的域
    https://example.com
    而不是
    https://example.com/path/to/appropriate/file
    我肯定会使用不包含路径字符之类的东西
    ^肯定会有用的
    嘿,乔尔,你所做的真是难以置信!这是如此接近完美,但唯一的问题是链接在段落或跨度内,因此它们中的一些没有类。这就是为什么我试图在身体上循环,但是你的代码好多了*编辑-谢谢你解释代码,我一定会从中学习!它们可以在任何地方运行(RegEx端),我只是不知道你是如何得到这个列表的。只需将
    preLinks
    视为对象数组,其中包含您希望匹配/使用的对象。:)请记住,有多种方法可以验证它是否是一个好的链接,但我将其保留为开放式供您使用。如果要检查
    .com/.net/etc
    有一个表达式。这假设链接是好的,并且文本在那里。考虑更多的用例会变得更复杂,所以我保持简单链接将始终有效并包含:http(s),一个文件路径。我可能还应该把它添加到OP中。如果我很难与Lol一起工作,那就很抱歉,一点也不!这只是需要考虑的重要因素,否则解决方案可能无法按预期工作。:)
    <div class="tnc"> By making a booking, You are agreeing to the  *https://digital.nhs.uk/about-nhs-digital/terms-and-conditions^terms and conditions* and will follow the COVID-19 precautions set in the email confirmation </div> 
    
    <div class="tnc"> By making a booking, You are agreeing to the  <a href="https://digital.nhs.uk/about-nhs-digital/terms-and-conditions">terms and conditions</a> and will follow the COVID-19 precautions set in the email confirmation </div> 
    
    const regex = /\*(.*[.].*)\^(.*)\*/;
    
    $("body").children().each(function () {
      var preText = this.innerHTML;
      var matches = regex.exec(preText);
      var a = document.createElement('a');
      a.href = matches[1];
      a.text = matches[2];
    
      const newText = matches.input.replace(matches[0], a);
    
      this.innerHTML = newText;
    });