简体   繁体   English

将所有文字包装在另一个标签中

[英]Wrap all text outside tags in another tag

My html can look (and sometimes looks) like this: 我的html可以看起来(有时看起来)像这样:

<p>Some text <b>some <i>more text</i></b></p>a little bit more text

I need to wrap in spans every word within these tags. 我需要将这些标签中的每个单词都包裹起来。 So the wrapped words would be Some, text, some, more, text, a, little, bit, more, text. 因此,换行的单词将是Some,text,some,more,text,a,一点,一点,更多,文本。 I tried to use this code: 我尝试使用此代码:

html_variable.replace(/(?<=>)[^<>]+(?=<)/g,filter)

where filter is a custom function, using which I easily wrapped every word. filter是一个自定义函数,使用它可以轻松包装每个单词。 However, it turned out that (?<=>), a positive lookbehind, is not yet supported in ios. 但是,事实证明,ios中尚不支持(?<=>),这是一个积极的后盾。 I am really bad in regular expressions, so I am asking for your help. 我的正则表达式确实很糟糕,所以我要求您的帮助。 Is there any well-supported alternative to the code above? 上面的代码是否有任何受支持的替代方法?

Try with this. 试试这个。

\w+(?![^<>]*>)

See demo here 在此处查看演示

This may work for you, depending in your input. 可能对您有用,具体取决于您的输入。 There are plenty of cases that will make this fail replacing content you don't want to replace (for example content of script and title tags) 在很多情况下,替换您不希望替换的内容(例如scripttitle标签的内容)将导致失败

Anyways, for a throw-away job and a limited input, It may work for you . 无论如何,对于临时工作和有限的投入, 它可能对您有用

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

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