我有一个带bbcodes的字符串。我需要通过[p]标签将字符串拆分为数组。字符串示例:
[p][b][size=3][br][/size][/b][size=3][b]COMPANY INFORMATION[/b]
[/size][br][ul][li][size=3]PM1[/size][/li][li][size=3]Pure[/size]
[/li][li][size=3]Each of these agreements.[/size][/li][li][size=3]
[url=/4b63cf8214dbd6c3ef17128e8fb5a49101414715.jpeg]Capital
Structure[/url][/size][/li][li][size=3]Capital[/size][/li][/ul][/p]
[p]fdgdfgdfg dfgdfkgjdf;glk
khghjghjghjghjghjghjghjghjghjj [url=/fghfghfgh.jpg]
[size=3]Jira[/size][/url][/p][p]sgsdfsdf sdf sf sdf sdf s dfs dfs
df ffkfkgkdkfgjdsfggggk gkgk sdfigjdsufioguds gfgigigigig ggigig ig[/p]
结果,我想要一个像这样的数组:
[
0: "[b][size=3][br][/size][/b][size=3][b]COMPANY INFORMATION[/b][/size][br][ul][li][size=3]PM1[/size][/li][li][size=3]Pure[/size][/li][li][size=3]Each of these agreements.[/size][/li][li][size=3][url=/4b63cf8214dbd6c3ef17128e8fb5a49101414715.jpeg]Capital Structure[/url][/size][/li][li][size=3]Capital[/size][/li][/ul]"
1: "fdgdfgdfg dfgdfkgjdf;glk khghjghjghjghjghjghjghjghjghjj [url=/fghfghfgh.jpg][size=3]Jira[/size][/url][/p][p]sgsdfsdf sdf sf sdf sdf s dfs dfs df ffkfkgkdkfgjdsfggggk gkgk sdfigjdsufioguds gfgigigigig ggigig ig"
...
]
现在,我只能这样做:
string.split(/[\[\]]/g).filter((value) => {return value !== ""}
它通过结果数组的单独元素
给我所有标签答案 0 :(得分:0)
我用正则表达式来解决它。我使用了一个捕获组,该组将返回[p]
和[/p]
之间的所有内容。
const
inputString = '[p][b][size=3][br][/size][/b][size=3][b]COMPANY INFORMATION[/b][/size][br][ul][li][size=3]PM1[/size][/li][li][size=3]Pure[/size][/li][li][size=3]Each of these agreements.[/size][/li][li][size=3][url=/4b63cf8214dbd6c3ef17128e8fb5a49101414715.jpeg]Capital Structure[/url][/size][/li][li][size=3]Capital[/size][/li][/ul][/p][p]fdgdfgdfg dfgdfkgjdf;glk khghjghjghjghjghjghjghjghjghjj [url=/fghfghfgh.jpg][size=3]Jira[/size][/url][/p][p]sgsdfsdf sdf sf sdf sdf s dfs dfs df ffkfkgkdkfgjdsfggggk gkgk sdfigjdsufioguds gfgigigigig ggigig ig[/p]',
paragraphRegex = /\[p\](.*?)\[\/p\]/gi,
paragraphs = [];
let
match;
while ((match = paragraphRegex.exec(inputString)) !== null) {
// The result of the capture group is at index 1 of the match.
paragraphs.push(match[1]);
}
console.log(`Number of paragraphs found: ${paragraphs.length}`);
paragraphs.forEach((paragraph, index) => console.log(`Paragraph ${index}: "${paragraph}"`));