【问题标题】:How to extract href attribute from link and create a specific pattern of that?如何从链接中提取 href 属性并创建一个特定的模式?
【发布时间】:2015-12-22 11:55:52
【问题描述】:

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

示例 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 是一个关键字。如果链接名称是untitled,我想创建一个常规 URL,但如果不是,则创建一个基于模式的 URL。

注意:模式=[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

【问题讨论】:

  • 输入字符串总是那个短格式?我认为不是,知道这一点很重要。您想解析整个 html 文档来执行此操作吗?
  • @RafałŁużyński 是的,不是的......!

标签: javascript jquery regex url


【解决方案1】:

我不明白。你所做的是对的。以下是您的解决方案运行良好:

$(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);
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;

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

$(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();
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 对不起。因为你的答案不是解决方案。这是一种解决方法。您已将我的两个示例实现为静态 ..!但我需要一个动态代码来调查每个字符串的链接名称 (是否为 untitled 关键字) 的值。
  • @Shafizadeh 你能进一步解释一下吗?我有兴趣解决它。
  • 我想要这样的东西:if (linkname == 'untitled') {// regular URL} else {// patter-based URL}
  • @Shafizadeh 已更新。请看上面的代码。 :)
  • 看起来不错。但我不知道为什么在我的真实代码中不起作用。反正好的谢谢。我接受您的解决方案作为我的答案,如果我遇到有关它的问题,我稍后会问您:-)
【解决方案2】:

你只需要检查元素的文本内容:

var newStr = $('<div/>', {
  html: str
}).find("a").replaceWith(function() {
  var
    href = this.getAttribute('href'),
    text = this.textContent
  ;

  return text === 'untitled' 
         ? href 
         : "[" + text + "](" + href + ")";

}).end().text();

【讨论】:

  • 哦还有一件事,作为咨询:我想使用这个js-library。现在我在我的代码中使用它,比如this。我想知道的是,我的代码没问题吗?在函数中定义对象是否优化?或者我可以做得更好? *(事先)谢谢。
  • @Shafizadeh 欢迎您。您的代码没有任何问题,它的性能对我来说很好。但是,您也可以在函数之外初始化实例。 jsfiddle.net/vhd8qy6k/4。这是一个相关的问题:stackoverflow.com/questions/385506/…
猜你喜欢
  • 2018-03-02
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
相关资源
最近更新 更多