如果 http:// 不存在於包含多個 URL 的字符串中,如何添加它

維蘭德拉遊行

我正在檢查的字符串:

   Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) 
    into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk
    Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. 
    Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. 
    this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) 
    Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total.
    Brush one end of each rectangle with a little of the beaten egg

上面是我檢查 URL 的字符串,如果 URL 沒有 http 或 https 我正在使用下面的邏輯添加它,但這裡的問題是它只是檢查第一個 URL,如果 http 或 https 不存在則替換它沒有其他沒有 http 或 https 的 URL 已更改

以下是我應用的邏輯:

  async urlify(text) {
   var urlRegex = new RegExp(
  '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?'
   );
  return await text.replace(urlRegex, (url) => {
   if (url.search(/^http[s]?\:\/\//) === -1) {
    return 'http://' + url;
   } else {
     return url;
   }
  });
  }

任何人都可以讓我知道這裡有什麼問題嗎?

thchp

使用g修飾符。沒有它,您的正則表達式只會找到第一個匹配項。此外,String.replace()不返回承諾。你不需要await.

const str = ` Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) 
    into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk
    Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. 
    Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. 
    this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) 
    Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total.
    Brush one end of each rectangle with a little of the beaten egg`;
    
function urlify(text) {
   var urlRegex = new RegExp(
    '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?', 
    'g'
   );
  return text.replace(urlRegex, (url) => {
   if (url.search(/^http[s]?\:\/\//) === -1) {
    return 'http://' + url;
   } else {
     return url;
   }
  });
}
  
console.log(urlify(str));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果问号已经存在,如何在URL中添加随机字符串以防止缓存?

如果 url 不包含子字符串,如何将路径添加到 url

如果不存在,则将http://添加到NSURL

如果不存在,如何在JavaScript中修改URL?

如果存在则替换以字符串开头的文件中的行,如果不存在则添加

如果不存在纯正则表达式替换,则在URL查询字符串中插入参数

如何拒绝访问 url,如果它包含字符串“.html”(使用 htaccess,我想获得 404 )

如何将.jpg添加到字符串末尾(如果不存在)

如果字符串存在,如何根据字符串数组在字符串中添加空格

如果對象存在於 JSON 中,則打印另一個值,如果不存在,則不打印任何內容

如果不存在,则在字符串开头添加子字符串(php)

如果某些字符串存在而某些字符串不存在,如何匹配多行

如果第一個鍵不存在於其中一個數據框中,則 Scala/Spark 加入第二個鍵

如果字符串在 Swift 中包含空格,如何搜索它?

{fmt}:如果格式化字符串中不存在命名参数,是否会忽略它?

如果JSON字符串中不存在属性,如何返回空字符串?

如果字符串在大写和小写中包含相同的字符,如何在哈希集中添加字符串值?

如果不存在http或https,则将react中的外部链接附加到本地主机

Ruby在轨道上。如果不存在,则将facebook.com/添加到URL

如果属性不存在,则显示空字符串

如果存在,则生成脚本,但如果不存在,则不包含在SQL Server中

如果表格beautifulsoup和pandas中不存在类,则停止爬网URL

如果行包含字符串,如何添加列?

SQL触发器,如果不存在则添加一个新字符串

如果文件不存在,则将字符串添加到文件底部

如果url不包含数组中的字符串,则执行函数

如果URL包含字符串A但不包含字符串B,则重定向

熊猫:添加列(如果不存在)

添加列(如果不存在)