开发者

How to extract a small string before a colon from a variable with jQuery and Regex?

开发者 https://www.devze.com 2023-03-03 15:53 出处:网络
I have a JavaScript variable similar to: \"a_name_string: RT @<a href=\"http://twitter.com/SomethinG\">SomethinG</a>: Lorem ipsum dolor sit amet - <a href=\"http://www.example.com/new

I have a JavaScript variable similar to:

"a_name_string: RT @<a href="http://twitter.com/SomethinG">SomethinG</a>: Lorem ipsum dolor sit amet - <a href="http://www.example.com/news.aspx?id=12645">http://www.example.com...</a>"

I want to grab "a_name_string:" (without the colon) and put it in a new variable (eg. name).

I am using jQuery.

Here is the code I have so far (it takes a local cache of a twitter rss feed and parses it into a pretty feed):

function nha_twitter(feed_name, html_id, qty) {
    //var feed_url = '/nha/cache/alertmessages.xml';
    var Pgurl = '/admin/twitter_feeds/' + feed_name + '.xml';
    /*
    $.ajax({
        type: 'GET',
        dataType: 'XML',
        url: feed_url,
        success: parse_xml
    });
    */

    var TWITTER_XML = Load_variable_Xml(Pgurl);
    parse_xml(TWITTER_XML, html_id, qty)

}  

    function parse_xml(xml, html_id, qty) {
        $(xml).find('item:lt(' + qty + ')').each(function(){
            var title = $(this).find('title').text();
            var desc = $(this).find('description').text();
            var pubDate = $(this).find('pubDate').text();
            var link = $(this).find('link').text();

            //alert($(title).val());

            //alert(title);

            //title = ify.clean(title);
            desc = ify.clean(desc);

            $('<li></li>').html('\n'+desc+'<br />\n'+pubDate+'<br />\n<a href="'+link+'">View on Twitter &raquo;</a>').appendTo(html_id);
        });
    }  // END parse_xml()

I guess I need a开发者_如何学Go regex but I am struggling...

Any help would be much appreciated.


As it seemed to work, I will put it as an answer ;)

If it is always the fist part, you can use .split():

var name = str.split(':')[0];
0

精彩评论

暂无评论...
验证码 换一张
取 消