1
\$\begingroup\$

I'm trying to "sanitize" a textarea input by disallowing repeated characters like "!!!", "???", etc. add spaces after commas, and I would like to optimize the code because I'm not a pro at this.

jsFiddle

$("#post_input").keypress(function() {
        var obj = this;
        setTimeout(function() {
            var text = obj.value;
            var selStart = obj.selectionStart;
            var newText = text.replace(/,{2,}|\.{4,}|\!{4,}|\¡{4,}|\?{4,}|\¿{4,}/, function(match, index) {
                if (index < selStart) {
                    selStart -= (match.length - 4);  // correct the selection location
                }
                return(match.substr(0,1));
            });
            if (newText != text) {
                obj.value = newText;
                obj.selectionStart = obj.selectionEnd = selStart;
            }
        }, 1);
    });

    $("#post_input").blur(function(){
      this.value = this.value.replace( /,\s*/g, ', ' );
    });​

I would like to merge all in only one regex and function.

Basically, I want to:

  • Prevent repeated characters (all kind of characters) to 3. No "aaaaa" or "!!!!!"
  • Add space after commas (,) or any kind of punctuation (!) (?) (.)
\$\endgroup\$

migrated from stackoverflow.com Jun 19 '12 at 15:52

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Why do you want to turn it all into one big ugly regex? \$\endgroup\$ – David B Jun 19 '12 at 15:28
  • \$\begingroup\$ @DavidB is there any better solution? I just want to make a clean code. \$\endgroup\$ – Santiago Jun 19 '12 at 15:29
  • \$\begingroup\$ No one will ever complain about the number of carriage returns in your code. Morphing validation into one gigantic, ugly regex will just make it harder to maintain. \$\endgroup\$ – David B Jun 19 '12 at 15:34
  • \$\begingroup\$ In "prevent repetead characters to 3", define "prevent" and "repetead characters to". \$\endgroup\$ – Qtax Jun 19 '12 at 15:35
  • \$\begingroup\$ Thanks David, I see your point. @Qtax I don't want to allow repetead any characters to avoid things like "helloooooo" or "WTF!!!!!!!!". Just limit the repetition to 3 max... \$\endgroup\$ – Santiago Jun 19 '12 at 15:37
2
\$\begingroup\$

Prevent repetead characters (all kind of characters) to 3.

If you mean replace 4 or more repeated characters with single character:

str.replace(/(.)\1{3,}/g, '$1');

Add space after commas (,) or any kind of punctuation (!) (?) (.)

str.replace(/[,.!?:;](?=\S)/g, '$& ');
\$\endgroup\$
  • \$\begingroup\$ Great! With the second regex the problem is that it adds spaces if there are 3 of the same punctuation, like "..." it converts it to ". . ." \$\endgroup\$ – Santiago Jun 19 '12 at 15:39
  • 1
    \$\begingroup\$ @Santiago, add a +, like [,.!?:;]+(?=\S). \$\endgroup\$ – Qtax Jun 19 '12 at 15:40
  • \$\begingroup\$ The 1st regex would work for same consecutive characters (eg. @@, .. etc), but what about different consecutive characters from a set (eg. @.) ? \$\endgroup\$ – Nikhil Nanjappa Nov 7 '18 at 13:55
  • \$\begingroup\$ @NikhilNanjappa consecutive characters from a set could be matched with /[@.]{2,}/. If you want replace it with the first matching character use /([@.])[@.]+/g in the first statement. \$\endgroup\$ – Qtax Nov 7 '18 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.