【发布时间】:2016-01-05 11:33:19
【问题描述】:
我的 HTML 中有如下所述的多个标签。请注意方括号,如 BBcode。
[oembed]http://rich-media.url[/oembed]
我需要使用 jQuery 获取 URL 的值。任何帮助将不胜感激。
【问题讨论】:
标签: jquery custom-tag
我的 HTML 中有如下所述的多个标签。请注意方括号,如 BBcode。
[oembed]http://rich-media.url[/oembed]
我需要使用 jQuery 获取 URL 的值。任何帮助将不胜感激。
【问题讨论】:
标签: jquery custom-tag
请查看更新的小提琴:
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;
}
}
我使用简单的 javascript 字符串函数来获取所需的 url。希望对您有所帮助。
【讨论】:
您可以使用正则表达式来检索大括号之间的内容,如下所示:
var content = '[oembed]http://rich-media.url[/oembed]';
var matches = /\[.+\](.+)\[.+\]/g.exec(content);
console.log(matches[1]); // = 'http://rich-media.url'
【讨论】:
试试这个代码
var content = $('#mytags').html();
var matches = /\[.+\](.+)\[.+\]/g.exec(content);
content.replace(/\[.+\](.+)\[.+\]/g, function(m, key, value){
//this is url
console.log(key);
});
【讨论】: