2

I have this string and I want to extract the TEXT1, TEXT2, etc... to an array m.

I tried with regex, but it doesn't work.

var regex = /item\b[^>]*>(.*?)<button/g; 

var string = '<li id="reminder-1440746981190" class="new-item">TEXT1
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>
<li id="reminder-1440746983703" class="new-item">TEXT2
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>';

var m;

while ((m = regex.exec(string)) !== null) {
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
}

alert(m[0]);

Any ideas?

1
  • I was going to suggest var splts = string.split(/<[^<]*>(?:\s*<[^<]*>)*/g); console.info(splts.filter(Boolean)); but I guess JQuery-based answer is cooler :) Commented Aug 28, 2015 at 12:02

2 Answers 2

3

You can try like this:

var str = $('li').contents().filter(function(){
    return this.nodeType === 3;
}).text();

console.log(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="reminder-1440746981190" class="new-item">TEXT1
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>
<li id="reminder-1440746983703" class="new-item">TEXT2
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>

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

2 Comments

However, this code does not provide the tag contents as an array. Please check the log.
You're right. It outputs a string. How do I convert this string to an array of elements?
1

Here is a piece of code based on @Rahul's answer that returns an array of trimmed text nodes:

var arr = [];
$('li').contents().filter(function(){
      if (this.nodeValue !== null && 
           this.nodeValue.trim().length > 0) {
        arr.push(this.nodeValue.trim());
     }
});
console.log(arr.filter(Boolean));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="reminder-1440746981190" class="new-item">TEXT1
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>
<li id="reminder-1440746983703" class="new-item">TEXT2
    <button class="icon-trash delete-button" contenteditable="false"></button>
    <button class="icon-pencil edit-button" contenteditable="false"></button>
</li>

Result: ["TEXT1", "TEXT2"]

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.