CodeIgniter Forums

Full Version: [Solved] finding number question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;
}

});
Try this in your regular expression pattern:
Code:
\[(\d+)\.?\]
The ? after the dot makes it optional.
Put the \d+ part between round brackets, in order to catch the mathes.
A good website to test regex:
https://regex101.com/
(01-14-2017, 12:27 PM)Wouter60 Wrote: [ -> ]Try this in your regular expression pattern:
Code:
\[(\d+)\.?\]
The ? after the dot makes it optional.
Put the \d+ part between round brackets, in order to catch the mathes.
A good website to test regex:
https://regex101.com/

Thanks that regex101 is good site i tried it out never herd of it before