使用JavaScript创建文本片段

时间:2014-02-13 15:27:21

标签: javascript text split fragment

我有一个如下字符串 -

var text="this is <b>a sample</b> long text. <i>Sample <b>tex</b>t <u>containing</u> some</i> bold underlined and it<i>alics wo</i>rds."

我想要一个像这样的JavaScript数组

var textArr = [
  "this is ",
  "<b>a sample</b>",
  " long text. ",
  " ",
  "<i>Sample</i>",
  " ",
  "<i><b>tex</b></i>",
  "<i>t</i>",
  " ",
  "<i><u>containing</u></i>",
  " ",
  "<i>some</i>",
  " bold underlined and it",
  "<i>alics wo</i>",
  "rds" 
];

我需要这个,因为我需要每个片段,以便我可以创建格式化的画布文本对象并将它们放在画布上。 有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

var text="this is <b>a sample</b> long text. <i>Sample <b>tex</b>t <u>containing</u> some</i> bold underlined and it<i>alics wo</i>rds."

var t = document.createElement("div")
t.innerHTML = text;

var textArr = t.childNodes;