Reputation: 504
I have the multiple tags as mentioned below in my HTML. Note the square brackets like BBcode.
[oembed]http://rich-media.url[/oembed]
I need to get the value of URL using jQuery. Any help would be appreciated.
Upvotes: 0
Views: 30
Reputation: 1713
Try this code
var content = $('#mytags').html();
var matches = /\[.+\](.+)\[.+\]/g.exec(content);
content.replace(/\[.+\](.+)\[.+\]/g, function(m, key, value){
//this is url
console.log(key);
});
Upvotes: 1
Reputation: 11512
Please have a look at updated fiddle:
https://jsfiddle.net/hxa4m8Ld/1/
var content = $('#mytags').text();
var currIndex = 0;
while(currIndex < content.length){
var stIndex = content.indexOf("[oembed]",currIndex);
var edIndex = content.indexOf("[/oembed]",stIndex);
if(stIndex > -1 && edIndex> -1){
var url = content.substring(stIndex+8,edIndex);
currIndex = edIndex+9;
console.log(url);
}
else
{
currIndex = content.length;
}
}
I used simple javascript string functions to fetch the required urls. Hope that will help you.
Upvotes: 1
Reputation: 337627
You can use a regular expression to retrieve the content between the braces, something like this:
var content = '[oembed]http://rich-media.url[/oembed]';
var matches = /\[.+\](.+)\[.+\]/g.exec(content);
console.log(matches[1]); // = 'http://rich-media.url'
Upvotes: 1