Jquery Extract URL from Text

2019-03-01 14:53发布

问题:

I need to extract URL from a text using jquery.

Lets say i have sowhere on the page following textarea code

<textarea rows="20" name="textarea" style="width:100%;">
   @techreport{blabl,  
   blabla = {},  
   url = {http://server.com/thepdf.pdf},  
   wrongurl ={http://server.com/thepdf2.pdf}, 
   blablabla = 1998,  
   blablablabla= {blablablablabla}}
</textarea>

i need the url, and only the url contents - not wrongurl.

Update: it has always the same structure and i only need to extract it ONCE and it always has an "url = {" in front of it.

回答1:

how about this

$(document).ready(function() {

    $('#click').click(function(){

 var one = document.getElementById('one');       
    one.value.match(/url ={([^}]*)}/,"");
    alert( RegExp.$1);


    })    
})

or a runnable demo http://jsfiddle.net/PePS7/10/

oops, bit late to the game but ammended the example and the jsfiddle to only show the url



回答2:

You're going to need to do some Reg ex on this. If I was better at them I'd write one up for you.



回答3:

jQuery won't do this, you're looking for a regular expression to extract the URL from the 'url' property in this textarea. You can do this with the following regex:

/url = \{(.+)\}/.exec(textarea_str)[1]



回答4:

The easiest thing to do is to use a regexp, like everyone has pointed to.

/url = \{([^}]*)\}/

That regexp should do it.