1

i wanted to substring a string something like follow. And i have start position and length of the string . i have checked this ques Substring text with HTML tags in Javascript. But in this substring is performing from 0 but i have a random start position. Any idea about how it will done

var str = 'Lorem ipsum <p>dolor <strong>sit</strong> amet</p>, consectetur adipiscing elit.'
2
  • @Alex i wanted the substring with its parent html tags. Commented May 31, 2013 at 12:27
  • jsfiddle.net/danmana/5mNNU check this fiddle in this all is done . But its working by assuming start position 0 . But in my case i have a random start position. Commented May 31, 2013 at 12:34

2 Answers 2

6

Here is DEMO

function html_substr( str, start ,count ) {

    var div = document.createElement('div');
    div.innerHTML = str;

    walk( div, track );

    function track( el ) {
        if( count > 0 ) {
            var len = el.data.length;
            if(start<=len){
                el.data = el.substringData(start,len);
                start=0;
            } else{
                start-=len;
                el.data = '';
            }
            len = el.data.length;
            count -= len;
            if( count <= 0 ) {
                el.data = el.substringData( 0, el.data.length + count );
            }

        } else {
            el.data = '';
        }
    }

    function walk( el, fn ) {
        var node = el.firstChild;
        do {
            if( node.nodeType === 3 ) {
                fn(node);
            } else if( node.nodeType === 1 && node.childNodes && node.childNodes[0] ) {
                walk( node, fn );
            }
        } while( node = node.nextSibling );
    }
    return div.innerHTML;
}

Call it as

html_substr( str,13, 2 );
Sign up to request clarification or add additional context in comments.

1 Comment

I have tested this quite thoroughly and it works every time. With nested tags and simple tags, as well as unclosed tags. To help understand the answer rahul has supplied here I will show the output of the three examples in the DEMO alongside the basic substr output 1) substr : p> ; html_substr : <p>ol<strong></strong></p> ---------- 2) substr : orem ipsum <p>dolor ; html_substr : orem ipsum <p>dolor <strong>sit</strong></p> ------------- 3) substr : rem ipsum <p>dolor <strong>sit ; html_substr : rem ipsum <p>dolor <strong>sit</strong> amet</p>, cons
0

Here is a native DOM API approach to do this sort of thing. The browser already has powerful built in HTML parsing abilities. No need to re-invent the wheel

var el = document.createElement("div");
el.innerHTML = 'Lorem ipsum <p>dolor <strong>sit</strong> amet</p>, consectetur adipiscing elit.';
el.getElementsByTagName("p").textContent;

Working demo

The result is just a non-html string which you can easily manipulate with .substring

So, how did this work?

We dumped our HTML into an HTML element.

Then we used the DOM API to say the following:

"Inside the HTML we just gave you, find all p tags, take the first (that's the [0]), and give us its textContent".

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.