1

I have a string like this:

var str = 'anything <span><a href="http://www.example.com/classname/methodname/arg1/arg2" target="_blank">...www.example.com/clas</a> </span> anything';

I want this output:

var newstr: 'anything http://www.example.com/classname/methodname/arg1/arg2 anything';

How can I do that? Actually I almost can do that using multiple str_replace(). But I want to know how can I do that using regex?

str.str_replace('<span><a href="', '');
str.str_replace('" target="_blank">', '');
str.str_replace('</a> </span> ', '');

// output:
anything http://www.example.com/classname/methodname/arg1/arg2...www.example.com/clas anything

Well, As you see, I just can't remove ...www.example.com/clas in the my output.

Note: anything in the example could be any words/sentences. Btw, in reality there is Persian characters.


Edit1: Here is an example:

I want to replace this:

This <span><a href="www.example.com/classname/methodname/arg1/arg2/" target = "_blank"> ... www.example.com/clas</a> </span> is very good.

With this:

This www.example.com/classname/methodname/arg1/arg2/ is very good.

Edit2: Also I need to the value of that link. Something like this pattern:

[LinkName](LinkAddress)

Note: Just I want to create the above patter if the value of that link don't be "untitled".

Example1:

var str = 'anything <span><a href="http://www.example.com/classname/methodname/arg1/arg2" target="_blank">website name</a> </span> anything';

I want this:

var newstr: 'anything [website name](http://www.example.com/classname/methodname/arg1/arg2 anything)';

Example2:

var str = 'anything <span><a href="http://www.example.com/classname/methodname/arg1/arg2" target="_blank">untitled</a> </span> anything';

I want this:

var newstr: 'anything http://www.example.com/classname/methodname/arg1/arg2 anything';
stack
  • 10,280
  • 19
  • 65
  • 117
  • 2
    I'd _html parse_ the snippet and manipulate the parsed nodes. – Ram Dec 17 '15 at 15:46
  • 1
    I think that this question \ answer is what you are looking for: [http://stackoverflow.com/questions/19547008/how-to-replace-plain-urls-with-links-with-example](http://stackoverflow.com/questions/19547008/how-to-replace-plain-urls-with-links-with-example) You can also take a look here: http://jsfiddle.net/EwzcD/1/ – Gen4ik Dec 17 '15 at 15:49
  • @Vohuman the tags in the question are the answer you are looking for – AGE Dec 17 '15 at 15:55
  • 2
    @AGE I can see those tags, indeed. His previous question was a PHP question and there is a PHP `str_replace` function in one of the snippets. That's why I had asked that question. – Ram Dec 17 '15 at 15:58

2 Answers2

6

You can wrap str inside DOM element, find any anchor tag, replace it with its own href attribute and then return the new string:

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

var str = 'anything <span><a href="http://www.example.com/classname/methodname/arg1/arg2" target="_blank">...www.example.com/clas</a> </span> anything';

var newStr = $('<div/>', {html: str}).find("a").replaceWith(function(){
  return $(this).attr('href');
}).end().text();
console.log(newStr);
$('body').append(newStr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • Well, I'm trying to figure out what you did, but I can't ... What is `'
    '` in your selector? I don't have any `
    ` tag in my string ...! I have `` and ``.
    – stack Dec 17 '15 at 16:00
  • @stack This creates a DIV element, setting its innerHTML to `str`. Then you can use any jq method on it – A. Wolff Dec 17 '15 at 16:03
  • Look, I add a new property in my website and now I faced with a new problem in this case. May you please take a look at **Edit2** in my question ? *(I edited my question recently)*. I think you can do that simply .. – stack Dec 22 '15 at 11:12
  • No and no, you have to post a new question on SO ;) – A. Wolff Dec 22 '15 at 11:14
  • Ok, I will write a new question, but give you a link of that when I posted it? Actually I want to see a answer from **you** .. – stack Dec 22 '15 at 11:16
  • Oops no, i didn't have watched carefully. Just be aware, i coudln't help you right now, i'll be busy – A. Wolff Dec 22 '15 at 11:18
1

If your text always varies, you should stick to Gen4tiks answer. However, if the rules for your sentences are always the way you described above, you could use the regex.split method explained here and define a regex that fits your needs. The following is a working but pretty stupid regex that accomplishes your needs, but is very useless for any other use-case. I just use it as an explanation how you can use the regexes:

var split = str.split(split(/(<\/*[a-zA-Z0-9=\\]+>)|(href=")|(target)/)
//split: ["anything interesting ", "<span>", undefined, undefined, "<a ", undefined, "href="", undefined, "http://www.example.com/classname/methodname/arg1/arg2" ", undefined, undefined, "target", "="_blank">...www.example.com/clas", "</a>", undefined, undefined, " ", "</span>", undefined, undefined, " anything else"]
var output = match[0] + match[(match.indexOf('href="')+2)] + match[match.length-1]
//output: "anything interesting http://www.example.com/classname/methodname/arg1/arg2"  anything else"
  • the / defines the start and end of your regex
  • the | defines the different option for when a split should take place
  • the <\/*[a-zA-Z0-9=\\]+> splits for every html-tag
  • the (href=") is used to find the correct index later (for the link you want to display)
  • the target splits the string at the position of the "target" attribute of your html (you dont want to have that in your output)

Hope that helps!

Remi Smirra
  • 2,499
  • 1
  • 14
  • 15