Javascript 拆分包括分隔符 [英] Javascript split include delimiters

查看:30
本文介绍了Javascript 拆分包括分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下正则表达式:

(</?[a-z][a-z0-9]*[^<>]*>)

我有以下文字:

<DIV><P class='abc'>Hello <B>Mister</B>! How are you >..< doing? </P>
<I>I'm good</I></DIV>

现在我想按标签拆分文本:

Now I'd like to split the text per tag:

<DIV>
<P class='abc'>
Hello 
<B>
Mister
</B>
! How are you >..< doing?

</P>
<I>
I'm good
</I>
</DIV>

如何使用 Javascript 正则表达式执行此操作?
是能够让它工作,但不得不重新开始,因为 javascript 不支持后视.

How can I do this with Javascript regex?
Is was able to get it to work but had to start over because javascript doesn't support lookbehinds.

(基本上在 html 标签上拆分并保留分隔符)

(basically split on html tags and keep the delimiters)


我的目标是使用 html 来存储格式.我想将上面的 html 提供给一个 javascript 对象.javascript 对象将格式与文本分开,并为格式化对象执行操作 A,为常规文本执行操作 B.


My goal with this is to use html to store formatting. I want to feed the html above to a javascript object. The javascript object separates the formatting from the text and does action A for formatting objects and action B for regular text.

我知道这听起来有点含糊,但我不想透露太多有关该项目的信息.

I know it sounds a bit vague, but I don't want to reveal too much about the project.

推荐答案

我实际上同意 Omar 在这个问题上的意见,但无论如何我都会给你正则表达式.:)

I actually agree with Omar on this issue, but I'll give you the regex anyway. :)

<[^>]+?>|.+?(?=(?:<[^><]+?>|$))

这篇关于Javascript 拆分包括分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆