Welcome Guest, Not a member yet? Register   Sign In
[Solved] finding number question
#1

(This post was last modified: 01-14-2017, 04:13 PM by wolfgang1983.)

On my codeigniter project I am making a js markdown script for my textarea

I have some jquery that can find numbers if wrapped like [1]


PHP Code:
var matches a.match(/(^|\n)\s*\[\d+\]:/g); 

But when I want to look for numbers like number with full stop next to it


Code:
1.


It can't find any. How can I make sure with the match above that it can find numbers with [1] and 1.

Code:
function findlistnumber(){
    var number = 1;

    var textarea = document.getElementById('message');

    var a = textarea.value;

    if(a.indexOf('1.') > -1){

        //Find lines with links
            
        var matches = a.match(/(^|\n)\s*\[\d+\]:/g);

        //Find corresponding numbers
            
        var usedNumbers = matches.map(function(match){
            return parseInt(match.match(/\d+/)[0]); }
        );

        //Find first unused number

        var number = 1;

        while(true){
                
            if(usedNumbers.indexOf(number) === -1){
                    
                //Found unused number
                    
                 return number;
            }

            number++;
        }
    }

    return number;
}

Full JS

Code:
$(document).ready(function() {

    var textarea_id = $('.panel-newthread .panel-body textarea').attr('id');

    //alert(textarea_id);

    $('#bold-button').on('click', function(e) {
        markdown_syntax(this.id, "**", "**", 'strong text');
    });

    $('#italic-button').on('click', function(e) {
        markdown_syntax(this.id, "*", "*", "emphasized text");
    });

    $('#quote-button').on('click', function(e) {
        markdown_syntax(this.id, "> ", "", "Blockquote");
    });

    $('#code-button').on('click', function(e) {
        markdown_syntax(this.id, "```", "```", "enter code here");
    });

    $('#h1-button').on('click', function(e) {
        markdown_syntax(this.id, "#", "", "");
    });

    $('#h2-button').on('click', function(e) {
        markdown_syntax(this.id, "##", "", "", "");
    });

    $('#h3-button').on('click', function(e) {
        markdown_syntax(this.id, "###", "");
    });

    $('#h4-button').on('click', function(e) {
        markdown_syntax(this.id, "####", "", "");
    });

    $('#h5-button').on('click', function(e) {
        markdown_syntax(this.id, "#####", "", "");
    });

    $('#h6-button').on('click', function(e) {
        markdown_syntax(this.id, "######", "", "");
    });

    $('#hr-button').on('click', function(e) {
        markdown_syntax(this.id, "---", "", "");
    });

    $('#olist-button').on('click', function(e) {
        if (findlistnumber()) {
            markdown_syntax(this.id, ' ' + findlistnumber() + ".", "", "List item");
        } else {
            markdown_syntax(this.id, " 1.", "", " List item");
        }
    });

    $('#ulist-button').on('click', function(e) {
        markdown_syntax(this.id, " - ", "", "List item");
    });


function markdown_syntax(button_id, syntax_open, syntax_close = '', empty_msg) {
    var textarea = $('#' + textarea_id);
   
    var len = textarea.val().length;
    var start = textarea[0].selectionStart;
    var end = textarea[0].selectionEnd;
    var selectedText = textarea.val().substring(start, end);

    if (selectedText.length > 0) {

        if (button_id === 'code-button') {

            replacement = syntax_open + '\n' + selectedText + '\n' + syntax_close;    

        } else {

            replacement = syntax_open + selectedText + syntax_close;

        }

    } else {

        if (button_id === 'code-button') {

            replacement = syntax_open + '\n' + empty_msg + '\n' + syntax_close;    

        } else {

            replacement = syntax_open + empty_msg + syntax_close;

        }

    }
      
    textarea.val(textarea.val().substring(0, start) + replacement + textarea.val().substring(end, len));
   
}

function findlistnumber(){
    var number = 1;

    var textarea = document.getElementById('message');

    var a = textarea.value;

    if(a.indexOf('1.') > -1){

        //Find lines with links
            
        var matches = a.match(/(^|\n)\s*\[\d+\]:/g);

        //Find corresponding numbers
            
        var usedNumbers = matches.map(function(match){
            return parseInt(match.match(/\d+/)[0]); }
        );

        //Find first unused number

        var number = 1;

        while(true){
                
            if(usedNumbers.indexOf(number) === -1){
                    
                //Found unused number
                    
                 return number;
            }

            number++;
        }
    }

    return number;
}

});
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
[Solved] finding number question - by wolfgang1983 - 01-14-2017, 02:06 AM
RE: finding number question - by Wouter60 - 01-14-2017, 12:27 PM
RE: finding number question - by wolfgang1983 - 01-14-2017, 04:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB