如何从链接中提取href属性并创建其特定模式? [英] How to extract href attribute from link and create a specific pattern of that?

查看:64
本文介绍了如何从链接中提取href属性并创建其特定模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两个例子解释我的问题:

I explain my question with two examples:

示例1:

当前字符串:

var str = 'anything <a href="www.google.com" target="_blank">untitled</a> anything';
//                                                               ^ link-name

我想要这个字符串:

var newstr = 'anything www.google.com anything';


示例2:

当前字符串:

var str = 'anything <a href="www.google.com" target="_blank">any thing else</a> anything';
//                                                                  ^ link-name

我想要这个字符串:

var str = 'anything [any thing else](www.google.com) anything';


如您在上面的两个示例中看到的那样,untitled是一个关键字.我想如果link-name是untitled,则创建该链接的常规URL,但如果不是,则创建该链接的基于模式的URL.


As you see in the two examples above, untitled is a keyword. I want if link-name is untitled, then create a regular URL of that, but if it isn't, then create a pattern-based URL of that.

注意:模式= [LinkName](LinkAddress)

我该怎么做?

这也是我尝试过的:

var newStr = $('<div/>', {html: str}).find("a").replaceWith(function(){
  return $(this).attr('href'); // this.href would give absolute path
}).end().text();

我的代码通过各种链接创建一个常规URL.我该如何添加该条件(检查链接名称是否为untitled)?

My code creates a regular URL from all kind of links. How can I add that condition (checking the link-name for being untitled or not) to that?

推荐答案

我不明白.您所做的是正确的.这是您的解决方案,效果非常好:

I don't understand. What you have done is right. Here are your solutions working perfectly fine:

$(function () {
  var str = 'anything <a href="www.google.com" target="_blank">untitled</a> anything';
  var newStr = $('<div/>', {html: str}).find("a").replaceWith(function(){
    return ($(this).text().trim().toLowerCase() == 'untitled') ? $(this).attr('href') : "[" + $(this).text() + "](" + $(this).attr('href') + ")";
  }).end().text();
  $("body").append(newStr + "<br /><br />");
  str = 'anything <a href="www.google.com" target="_blank">any thing else</a> anything';
  newStr = $('<div/>', {html: str}).find("a").replaceWith(function(){
    return ($(this).text().trim().toLowerCase() == 'untitled') ? $(this).attr('href') : "[" + $(this).text() + "](" + $(this).attr('href') + ")";
  }).end().text();
  $("body").append(newStr);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

更新的代码(更好的版本)

$(function () {
  var str = 'anything <a href="www.google.com" target="_blank">untitled</a> anything';
  var newStr = string_it(str);
  $("body").append(newStr + "<br /><br />");
  str = 'anything <a href="www.google.com" target="_blank">any thing else</a> anything';
  newStr = string_it(str);
  $("body").append(newStr);
});

function string_it (str) {
  return $('<div/>', {html: str}).find("a").replaceWith(function(){
    return ($(this).text().trim().toLowerCase() == 'untitled') ? $(this).attr('href') : "[" + $(this).text() + "](" + $(this).attr('href') + ")";
  }).end().text();
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

这篇关于如何从链接中提取href属性并创建其特定模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆