开发者

Jquery loop through custom tags

开发者 https://www.devze.com 2023-02-11 08:55 出处:网络
I\'m going to have a custom tag on my forum, something l开发者_StackOverflowike [FISH]data|data|data....[/FISH]

I'm going to have a custom tag on my forum, something l开发者_StackOverflowike

[FISH]data|data|data....[/FISH]

In Jquery, how do I loop through all instances of the [FISH] tag and get all the data between it so I can render? It must:

  • Match pairs only (IE, print an error or ignore [FISH] which has no corresponding [/FISH]
  • Be case insensitive


You can parse such tags with regular expressions. Try:

console.log($('body').text().match(/(\[FISH\][^\[]*\[\/FISH\])/g));

Here is a live snippet


As the commenter mentioned it is not possible using the square brackets but using <> brackets it is as simple as:

$("FISH")
0

精彩评论

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