gpt4 book ai didi

Javascript将字符串与模式匹配并在匹配的字符串中插入一些文本

转载 作者:行者123 更新时间:2023-11-30 17:58:40 25 4
gpt4 key购买 nike

输入:

 blah blah blah text blah <a href="/abcblah/blah">some random text</a> text blah blah random

action:匹配所有具有相对链接的 href 标签实例,然后插入主机 url。输出:

blah blah blah text blah <a href="http://www.rooturl.com/abcblah/blah">some random text</a> text blah blah random

我想知道如何在 javascript 中快速干净地执行此操作,需要正则表达式专家的帮助。非常感谢您的输入!

最佳答案

这个基于正则表达式的解决方案应该适合你:

str = 'blah blah text blah <a href="/abcblah/blah">some random text</a> text blah random';
repl = str.replace(/(href=['"](?!https?:))\/?/g, "$1http://www.rooturl.com/");
console.log(repl);

现场演示:http://ideone.com/G5F0vF

关于Javascript将字符串与模式匹配并在匹配的字符串中插入一些文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17577225/

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