简体   繁体   English

在bbcode标记之外的文本中添加标记

[英]Add tags to the text outside of bbcode tags

I have a problem that I can not answer. 我有一个我无法回答的问题。 I write an BBCODE editor with switch between a WYSIWYG editor and code editor. 我编写了BBCODE编辑器,并在WYSIWYG编辑器和代码编辑器之间切换。

The visual editor is built from a drag and drop blocs system (picture, text, ...) 可视编辑器是通过拖放式方块系统(图片,文本等)构建的

In the visual editor, when an user drag a new text bloc, the content is automatically written between [text][/text] tags. 在可视编辑器中,当用户拖动新的文本块时,内容将自动写入[text][/text]标签之间。

In the code editor, user can write free text without [text][/text] tags. 在代码编辑器中,用户可以编写没有[text][/text]标签的自由文本。

To be able to switch between the two editors, free text need to be added between [text][/text] tags in code editor. 为了能够在两个编辑器之间切换,需要在代码编辑器的[text][/text]标签之间添加自由文本。

Example : 范例

I write text and bbcode in code editor : 我在代码编辑器中编写文本和bbcode:

Cum haec taliaque sollicitas eius aures everberarent expositas semper eius modi
rumoribus et patentes.
[img]https://foo.com/fighters.png[/img]
Denique Antiochensis ordinis vertices sub uno elogio iussit occidi ideo efferatus,
quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia
[img]https://foo.com/fighters1.png[/img]
[img]https://foo.com/fighters2.png[/img]
Utque proeliorum periti rectores [i]primo catervas[/i] densas opponunt et fortes,
deinde leves armaturas, post iaculatores ultimasque subsidiales acies, si fors
adegerit

If i switch to visual editor, the free text need to be added between [text][/text] like this: 如果我切换到可视化编辑器,则需要在[text][/text]之间添加自由文本,如下所示:

[text]Cum haec taliaque sollicitas eius aures everberarent expositas semper eius modi
rumoribus et patentes.[/text]
[img]https://foo.com/fighters.png[/img]
[text]Denique Antiochensis ordinis vertices sub uno elogio iussit occidi ideo efferatus,
quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia[/text]
[img]https://foo.com/fighters1.png[/img]
[img]https://foo.com/fighters2.png[/img]
[text]Utque proeliorum periti rectores [i]primo catervas[/i] densas opponunt et fortes,
deinde leves armaturas, post iaculatores ultimasque subsidiales acies, si fors
adegerit[/text]

I think there are two ways: 我认为有两种方法:

  • Split text and bbcode with loops, and rebuild code with another loops. 使用循环分割文本和bbcode,并使用另一个循环重建代码。
  • Use a regex to get free text and replace it. 使用正则表达式获取自由文本并替换它。

What the best way ? 最好的方法是什么? Do you think that possible to add the tags from a regex ? 您认为可以从正则表达式添加标签吗?

Thank you, Thomas 谢谢托马斯

Try with this: 试试这个:

 const regex = /(\\[(img|\\w{4,})\\][\\s\\S]*?\\[\\/\\2\\])(\\n?)|([\\s\\S]+?)(\\n?)(?=$|\\[(?:img|\\w{4,})\\])/gi; let str = ` Cum haec taliaque sollicitas eius aures everberarent expositas semper eius modi rumoribus et patentes. [image]https://foo.com/fighters.png[/image] Denique Antiochensis ordinis vertices sub uno elogio iussit occidi ideo efferatus, quod ei celebrari vilitatem intempestivam urgenti, cum inpenderet inopia [image]https://foo.com/fighters1.png[/image] [image]https://foo.com/fighters2.png[/image] Utque proeliorum periti rectores [i]primo catervas[/i] densas opponunt et fortes, deinde leves armaturas, post iaculatores ultimasque subsidiales acies, si fors adegerit`; let m; let outstr = ''; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. // m[1] == structure tags // m[4] == text content // m[3] nad m[5] are new lines (if present) if (typeof m[1] != 'undefined') { outstr += m[1] + m[3]; } else { outstr += '[text]' + m[4] + '[/text]' + m[5]; } } console.log(outstr); 

On the regex, you use the first capturing group to get rid of the structural tags. 在正则表达式上,您使用第一个捕获组来摆脱结构标签。 And the second group for the rest of the data. 第二组为其余数据。 If the first group has data then it means we found an structural tag. 如果第一组有数据,则意味着我们找到了结构标签。 We just accumlate it. 我们只是积累它。 If not, then it means it is text. 如果不是,则表示它是文本。 So we accumulate it with the new [text] tags 因此,我们使用新的[text]标签对其进行了累积

Finally, on 3rd and 5th capturing group you have the new lines (if present) 最后,在第3和第5个捕获组中,您有新行(如果存在)

The second capturing group is used for making the opening and closing tag equal. 第二捕获组用于使开始和结束标签相等。

Demo on regex101 regex101上的演示

Regex explained: 正则表达式解释:

  # First option: an structural tag ([image]...[/image]
  ( # First capturing group
    \[ # Literal '['
      (img|\w{4,}) # img tag or tag with 4 or more letters (all structural tags)
    \] # Literal ']'
    [\s\S]*? # Any character 0 or more times, ungreedy
    \[\/\2\] # Closing tag. Word = same as opening tag
  )(\n?) # a new line may appear. Save it on third capturing group

  # Second option: other text
| ([\s\S]+?) # Any character 1 or more times, ungreedy. Third capturing group
  (\n?)      # A new line may appear, Don't want it on the previous group
  (?=        # Lookahead. The following must appear (but we don't match it)
       $  # Either end of line
     | \[(?:img|\w{4,})\] # or some opening structural tag
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM