1

I have a text containing HTML and my own special mark-up. Before I print the text, I need to know its length, so I can generate apropriate speech bubble.

I am trying to get the actual length of the string, but I am unable to get length of all special mark-ups.

He're an example of the string

var text = 'Lorem ipsum dolor sit amet [T1]. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquam accumsan elit, non vestibulum ex venenatis vitae. <br /><br />[D|1000] Hesitulus <br /> <span class="important"> l[D|100]o[D|100]r[D|100]e[D|100]m</span> <br />'

I am trying to get the length of all "[]"-type substrings using a regexp, but I am no master of that and I fail only having the first one. Tried few more options, but with no success.

var allMyMarkup = text.match( /(\[.*?\])/); // selecting only the first code

How to get all occurences of my pattern?

2 Answers 2

3

Use 'g' modifier like below:

text.match( /(\[.*?\])/g)

g modifier refers to 'global', which means, find all matches, not just the fist one.

Sign up to request clarification or add additional context in comments.

2 Comments

You can mark the answer as solved if it solves your problem :)
It definitely does, but I need to wait 10 minutes before I can do that. :)
0

Use:

document.querySelector('.your_element').innerHTML.length;

The innerHTML method strip the HTML tags.

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.