javascript - 正则表达式 - 检查字符串是否包含部分 URL 并转换为链接
问题描述
我只是无法理解正则表达式,这让我发疯。
我有以下字符串;
'This is a random string of text with a link to google.com/test and another link to facebook.com`
我想要做的是google.com/test
变成https://google.com/test
但将facebook.com
链接保留为纯文本。
所以基本上,google.com
(包括前缀)的任何实例都会变成一个链接,但任何其他 URL 都将保留为纯文本。
有人可以给我一个正确的方向吗?
解决方案
简单replace()
会做:
var str = 'This is a random string of text with a link to google.com/test and another link to facebook.com';
str = str.replace('google.com/test', 'https://google.com/test');
console.log(str);
推荐阅读
- html - 联系表格未显示在页脚中
- javascript - React Hooks + Firebase Firestore onSnapshot - 正确使用带有反应钩子的 Firestore 监听器
- elixir - 为什么我的 Elixir 函数返回列表列表?
- ms-word - 在 Word office 加载项中从当前位置搜索
- r - 意外输入 \ 不允许在字符串中识别字符
- java - 在spring boot中使用POSTMAPPING从“HTML表单”创建JSON后如何将数据插入数据库?
- libreoffice - 为 LibreOffice 开发自定义字段/变量
- python-3.x - Python3 Base64 编码 PyDes 3DES 对象
- docker - 如何将外部文件连接到 docker-nginx 容器?
- node.js - 为什么每次运行新的 Gatsby JS 构建时 LogRocket 都会记录会话