0

I have a string that I want to get text from:

var text = "<p>This is awesome and</p> <p>some random</p> and <custom>complex boring text</custom> <p>I</p> <custom>really need to clean</custom> up my room. that feel"

I want to get the text from all the <custom /> tags in a array. I am trying to get this result:

['complex boring text', 'really need to clean']

How can I do this?

1 Answer 1

3

Firstly, never ever use RegEx to parse HTML. To achieve this you can create a jQuery object from the text and then filter() out the custom elements before creating an array of their text using map(). Try this:

var arr = $(text).filter('custom').map(function() {
    return $(this).text();
}).get();
console.log(arr);

Working example

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

4 Comments

Thanks +1 for letting me know about regex to HTML
@Rory: Secondly: never answer "gimme-code" questions. Should “Give me a regex that does X” questions be closed?
@WiktorStribiżew I'd rather help someone than simply close a question for asking about an incorrect solution.
As people say, fair enough.

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.