Regex - Get x number of characters from a string but html tags does not count towards the count












2














As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.



Example string



Some <i>t</i>est <b>string</b> test


9 characters would output



Some <i>t</i>est


13 characters would output



Some <i>t</i>est <b>str


19 characters would output



Some <i>t</i>est <b>string</b> te


I'm not too sure where to go from the basic



(.*?){0,10}


Also, is there a way to make HTML encoded characters e.g. &lt; count as one character?










share|improve this question
























  • Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
    – Wiktor Stribiżew
    Nov 19 '18 at 11:51












  • That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
    – Adam Buckley
    Nov 19 '18 at 12:15












  • Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
    – Wiktor Stribiżew
    Nov 19 '18 at 12:17












  • Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
    – Adam Buckley
    Nov 19 '18 at 12:56










  • I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
    – Adam Buckley
    Nov 19 '18 at 13:03
















2














As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.



Example string



Some <i>t</i>est <b>string</b> test


9 characters would output



Some <i>t</i>est


13 characters would output



Some <i>t</i>est <b>str


19 characters would output



Some <i>t</i>est <b>string</b> te


I'm not too sure where to go from the basic



(.*?){0,10}


Also, is there a way to make HTML encoded characters e.g. &lt; count as one character?










share|improve this question
























  • Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
    – Wiktor Stribiżew
    Nov 19 '18 at 11:51












  • That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
    – Adam Buckley
    Nov 19 '18 at 12:15












  • Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
    – Wiktor Stribiżew
    Nov 19 '18 at 12:17












  • Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
    – Adam Buckley
    Nov 19 '18 at 12:56










  • I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
    – Adam Buckley
    Nov 19 '18 at 13:03














2












2








2







As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.



Example string



Some <i>t</i>est <b>string</b> test


9 characters would output



Some <i>t</i>est


13 characters would output



Some <i>t</i>est <b>str


19 characters would output



Some <i>t</i>est <b>string</b> te


I'm not too sure where to go from the basic



(.*?){0,10}


Also, is there a way to make HTML encoded characters e.g. &lt; count as one character?










share|improve this question















As the title suggests I need to get X amount of characters from a string but HTML is not including in the count.



Example string



Some <i>t</i>est <b>string</b> test


9 characters would output



Some <i>t</i>est


13 characters would output



Some <i>t</i>est <b>str


19 characters would output



Some <i>t</i>est <b>string</b> te


I'm not too sure where to go from the basic



(.*?){0,10}


Also, is there a way to make HTML encoded characters e.g. &lt; count as one character?







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:28









Wiktor Stribiżew

308k16126202




308k16126202










asked Nov 19 '18 at 11:48









Adam Buckley

184




184












  • Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
    – Wiktor Stribiżew
    Nov 19 '18 at 11:51












  • That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
    – Adam Buckley
    Nov 19 '18 at 12:15












  • Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
    – Wiktor Stribiżew
    Nov 19 '18 at 12:17












  • Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
    – Adam Buckley
    Nov 19 '18 at 12:56










  • I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
    – Adam Buckley
    Nov 19 '18 at 13:03


















  • Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
    – Wiktor Stribiżew
    Nov 19 '18 at 11:51












  • That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
    – Adam Buckley
    Nov 19 '18 at 12:15












  • Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
    – Wiktor Stribiżew
    Nov 19 '18 at 12:17












  • Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
    – Adam Buckley
    Nov 19 '18 at 12:56










  • I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
    – Adam Buckley
    Nov 19 '18 at 13:03
















Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
– Wiktor Stribiżew
Nov 19 '18 at 11:51






Try ^(?:(?:<[^<>]*>)*[^<]){9}, see demo.
– Wiktor Stribiżew
Nov 19 '18 at 11:51














That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 '18 at 12:15






That's great, although I have just thought a head, is there a way to make HTML encoded characters e.g. &lt; count as one character? also how do I upvote you on a comment?
– Adam Buckley
Nov 19 '18 at 12:15














Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
– Wiktor Stribiżew
Nov 19 '18 at 12:17






Like this? ^(?:(?:<[^<>]*>|&lt;.*?&gt;)*.){9}
– Wiktor Stribiżew
Nov 19 '18 at 12:17














Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
– Adam Buckley
Nov 19 '18 at 12:56




Very close I've changed it a little to ^(?:(?:<[^<>]*>)|(?:&[^s+]*;)*.){21} but I want the &.*; to count as one letter. any ideas?
– Adam Buckley
Nov 19 '18 at 12:56












I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
– Adam Buckley
Nov 19 '18 at 13:03




I've just worked it out ^(?:(?:&[^s+]*;)|(?:<[^<>]*>)*.){21} regex101.com/r/ecW1A4/1 change the number between 19 and 20 to see what I was aiming for. Thank you for your help.
– Adam Buckley
Nov 19 '18 at 13:03












1 Answer
1






active

oldest

votes


















1














To match a certain amount of chars from the string start excluding substrings like <...> from the count and counting &XX*; entities as one unit, you may use



^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}


See the regex demo.



Details





  • ^ - start of string


  • (?: - start of a non-capturing group that will serve as a containter for the alternatives below:



    • &[^s;]*; - a &, 0 or more chars other than whitespace and ; and then ;


    • | - or


    • (?:<[^<>]*>)*. - 0 or more repetitions of <, 0+ chars other than < and > and then >, and then any char other than a line break char (use DOTALL modifier to match any char with .)




  • ){20} - end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374009%2fregex-get-x-number-of-characters-from-a-string-but-html-tags-does-not-count-to%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To match a certain amount of chars from the string start excluding substrings like <...> from the count and counting &XX*; entities as one unit, you may use



    ^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}


    See the regex demo.



    Details





    • ^ - start of string


    • (?: - start of a non-capturing group that will serve as a containter for the alternatives below:



      • &[^s;]*; - a &, 0 or more chars other than whitespace and ; and then ;


      • | - or


      • (?:<[^<>]*>)*. - 0 or more repetitions of <, 0+ chars other than < and > and then >, and then any char other than a line break char (use DOTALL modifier to match any char with .)




    • ){20} - end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.






    share|improve this answer


























      1














      To match a certain amount of chars from the string start excluding substrings like <...> from the count and counting &XX*; entities as one unit, you may use



      ^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}


      See the regex demo.



      Details





      • ^ - start of string


      • (?: - start of a non-capturing group that will serve as a containter for the alternatives below:



        • &[^s;]*; - a &, 0 or more chars other than whitespace and ; and then ;


        • | - or


        • (?:<[^<>]*>)*. - 0 or more repetitions of <, 0+ chars other than < and > and then >, and then any char other than a line break char (use DOTALL modifier to match any char with .)




      • ){20} - end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.






      share|improve this answer
























        1












        1








        1






        To match a certain amount of chars from the string start excluding substrings like <...> from the count and counting &XX*; entities as one unit, you may use



        ^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}


        See the regex demo.



        Details





        • ^ - start of string


        • (?: - start of a non-capturing group that will serve as a containter for the alternatives below:



          • &[^s;]*; - a &, 0 or more chars other than whitespace and ; and then ;


          • | - or


          • (?:<[^<>]*>)*. - 0 or more repetitions of <, 0+ chars other than < and > and then >, and then any char other than a line break char (use DOTALL modifier to match any char with .)




        • ){20} - end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.






        share|improve this answer












        To match a certain amount of chars from the string start excluding substrings like <...> from the count and counting &XX*; entities as one unit, you may use



        ^(?:&[^s;]*;|(?:<[^<>]*>)*.){20}


        See the regex demo.



        Details





        • ^ - start of string


        • (?: - start of a non-capturing group that will serve as a containter for the alternatives below:



          • &[^s;]*; - a &, 0 or more chars other than whitespace and ; and then ;


          • | - or


          • (?:<[^<>]*>)*. - 0 or more repetitions of <, 0+ chars other than < and > and then >, and then any char other than a line break char (use DOTALL modifier to match any char with .)




        • ){20} - end of the container group and the limiting quantifier tells the engine to match 20 consecutive occurrences of its pattern.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:27









        Wiktor Stribiżew

        308k16126202




        308k16126202






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374009%2fregex-get-x-number-of-characters-from-a-string-but-html-tags-does-not-count-to%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules