1

I explain my question with two examples:

Example1:

current string:

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

I want this string:

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

Example2:

Current string:

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

I want this string:

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

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.

Note: pattern = [LinkName](LinkAddress)

How can I do that?


Also here is what I have tried:

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

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?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

2 Answers2

1

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>

Updated Code (better version)

$(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>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

You just need to check the text content of the element:

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();
Ram
  • 143,282
  • 16
  • 168
  • 197
  • Oh one thing else, as a consult: I wat to use of this [js-library](https://github.com/jgm/commonmark.js). Now I'm use it in my code like [this](https://jsfiddle.net/vhd8qy6k/2/). What I want to know is, my codes are fine? defining a object in the function is optimized? or I can do that better? *(beforehand) thanks. – Shafizadeh Dec 22 '15 at 13:28
  • 1
    @Shafizadeh You are welcome. There is nothing wrong with your code and it's performance is fine for me. However, you can also initialize the instances outside of the function. https://jsfiddle.net/vhd8qy6k/4/. And here is a related question: http://stackoverflow.com/questions/385506/when-is-optimisation-premature – Ram Dec 22 '15 at 13:45