1

I have a string like below -

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."

I want a JavaScript array like this

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" 
];

I need this because I need each fragment so that I can create that formatted canvas text object and place them on canvas. Can anyone please help me on this?

1
  • The second answer here seems pretty relevant: stackoverflow.com/questions/4247838/… Also, please use modern HTML tags, <strong> and <em> instead of <i> and <b>. Commented Feb 13, 2014 at 15:32

1 Answer 1

1

You can use this piece of code:

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.