tags:

views:

80

answers:

3

Hello,

I have these URLs from a text file

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url": 

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url":  }, 

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url": 

I want to do a loop, in #from and append it to #here

$('#from').match(/http:\/\/\S+/).each(function(){;
    var t = $(this).text();
    $(t).appendTo('#here');
});

There seems to be an error .

HTML CODE

<div id="from">

<? 
$fl = "file.txt"; 
$file1 = fopen($fl, "r");
$content = file_get_contents($fl); 
echo $content;       
?>

</div>

<div id="here"></div>

I could easily use PHP, but I want it to make it in jQuery, since it gives me the freedom to pull any http://, be it anywhere in the text file or how complicated it may be.

Thanks Jean

A: 

If you just want what look like links from the text then do something like:

var text = $("#form").text();
var r = /\b(http://.*?)(?=\s|\z)/;
var here = $("#where");
while (m = r.exec(text)) {
  $("<span>").text(m[1]).appendTo("#here");
}

Your question is pretty vague about what you're dealing with and what you want the end result to be so I've made a lot of assumptions:

  • This won't capture URLs in images or anchors.
  • I don't know what the destination is or how (or even if) you want to wrap the URLs. In my example I'm just putting each in a <span>;
  • I've been fairly liberal when it comes to interpreting what a URL is. I've assumed anything that starts on a word boundary with "http://" is a URL up to the end of the text or a white space character, whichever comes first. You can make this stricter if you wish.
cletus
@cletusError Message: Expected ')' in regular expressionFrom this line - var r = /\b(http://.*?)(?=\s|\z)/;
Jean
@Jean `var r = /\b(http://.*?)(?=\s|\z/);`
c0mrade
@c0mrade still the same error
Jean
@Jean var r = /\b(http:\/\/.*?)(?=\s|\z)/;
David Hedlund
@david hedlundThat is the line thats giving the error - Expected ')' in regular expression
Jean
@Jean: i corrected it. you won't get an error if you add the backslashes in the http part.
David Hedlund
@david its the sameYour corrected answer: var r = /\b(http://.*?)(?=\s|\z)/;Pre corrected answer: var r = /\b(http://.*?)(?=\s|\z)/;
Jean
@all please have a look at the edited Q
Jean
@Jean: no, my corrected answer has backslashes in the http. Pre-corrected: `http://`, corrected: `http:\/\/`. The corrected version works. The pre-corrected one doesn't.
David Hedlund
@david, its going into a loop, IE asks me to stop the script
Jean
@Jean: that may well be. i just corrected the regex so that it would compile. if it runs slow it's probably because the data does not look the way we expect it to, because you haven't described your situation very well. we still have no idea what #here and #from are, the little data you've shown looks corrupted, we haven't seen a single line of html, and you haven't *really* explained what you want to do, what you've tried, why that isn't working and what error message you're getting.
David Hedlund
Jean
A: 

You said:

I am trying to get all http links from #from and appending it to #here.

If I undestood you correctly, you need something like this:

$('#from a').appendTo('#here');

This will take all links (<a>-elements) from #from and move them to #here. Is this what you wanted?

If you don't want to move the links, but only copy them, you can use .clone():

$('#from a').clone().appendTo('#here');
Tatu Ulmanen
@all please have a look at the edited Q
Jean
Jean
A: 
$("#from a[href^='http']").appendTo('#here')
enodyt
it will not because the links are not <a href="http://adf">somelink</a>
Jean