0

I am new in JQuery and I am trying to get substring from main string.I know there is function available for that but I don't know how to do my task using that.

here is my string

//Page 1
<p>
    <l>  30,  St.Bishop Road,  30min </l> 
    <l>  10,  St.Bishop Road,  10min </l>
</p>

//Page 2
<p> 
    <l>  30,  St.Bishop Road,  30min </l> 
    <l>  10,  St.Bishop Road,  10min </l>
</p>

I want to separate and store each <p><p> in array and same way for each <p></p> I want to store <l></l> value in array.

Keep in mind that this is not html element.this is string which I get from other task.

so How can I do this using jquery ?

Thanks

5
  • what you want to store? and which substring you are looking for? Commented Sep 26, 2014 at 9:56
  • show a test string and what you would like to get from that string Commented Sep 26, 2014 at 9:57
  • what is your string and what is your sub-string..... Commented Sep 26, 2014 at 9:58
  • I want how many <p></p> combination as well as for each combination how many <l></l> available Commented Sep 26, 2014 at 9:58
  • correct your markup as closing <l> should be like </l> Commented Sep 26, 2014 at 9:59

3 Answers 3

1

You can convert any valid HTML-like string into a jQuery object and you can then treat it as normal:

var $el = $('<p><l>30 St....');
console.log( $el.find('p') );
console.log( $el.find('p').length );
console.log( $el.find('l') );
console.log( $el.find('l').length );
Sign up to request clarification or add additional context in comments.

Comments

0

NOTE: I wrote this before OP edit, his markup was not valid and he stated that it was not html.

I'm trying to keep this simple. You don't need jQuery to do this, just use String.split():

The split() method splits a String object into an array of strings by separating the string into substrings.

This does what you need:

var pieces = str.split('<p>');

Note that every piece will end with </p>, use String.substr() to remove that:

var piece = pieces[0].substr(0, pieces[0].length-4));

Do the same for <l> elements.

3 Comments

I could be biased, but that feels much less simple than just having jQuery parse it into a selector :-)
Looking at the first post, it is :-)
this will create one unnecessary array element at first place which will always blank.
0

I thing you expect this

    $(document).ready(function(){
   var c="<p> <l>  30,  St.Bishop Road,  30min <l> <l>  10,  St.Bishop"+ "Road,10min <l>"+
"</p>"+
"<p>"+ 
    "<l>  30,  St.Bishop Road,  30min <l>"+ 
    "<l>  10,  St.Bishop Road,  10min <l></p>";

    var b=c.split("<p>");
    $(b).each(function(data,value){
        if(data==1){

            var c=(value.split("<l>"))[1];
            alert(c);
        }
        if(data==2) {
            var d=(value.split("<l>"))[1];
            alert(d);
        }
    });



});

Demo

3 Comments

His HTML is in a string, it's not part of the DOM
I want to create 2 arrays one is for <p></p> and one is for <l></l>.so this solution will not work.
@sandy,I changed the link,check it.

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.