Rahul Singh
Rahul Singh

Reputation: 1632

Jquery function to format textarea data

I have created a jquery function to format my text box data....such that I have a formatted output to my input box.

jQuery.fn.singleDotHyphen = function(){
    return this.each(function(){
        var $this = $(this);
        $this.val(function(){
            return $this.val()
                .replace(/\.{2,}/g, '.')
                .replace(/\'{2,}/g, "\'")
                .replace(/\,{2,}/g, ",")
                .replace(/\n{3,}/g, "\n\n")
                .replace(/\s{2,}/g, " ")                
                .replace(/-{2,}/g, '-');    
        });
    });
};

I have created it for newline that if more than 3 new lines are present than it convert it to 2 newline. But issue is if it find more than 3 nwe line than it convert it two space. How can I differentiate in both constraints that is of space and newline.

Upvotes: 0

Views: 226

Answers (1)

Anders Marzi Tornblad
Anders Marzi Tornblad

Reputation: 19305

\s matches any white-space character, including space, tab and linefeed.

Switch from /\s{2,}/g to / {2,}/g if you want to match literal spaces only.

Upvotes: 1

Related Questions