2

What I am trying to do is clean up an input field on blur. My blur code is completely functional, but I can't get the regex to work correctly. All of the characters I'm trying to allow are working correctly except for any number of line breaks. I've tried with different variations and combinations of including /s, /r, and /n .

I am doing this because I want to prevent as many characters that don't really belong in a descriptive input field as possible. I am using entity to linq for database input, which should protect me from sql injection attacks, but I still want to restrict the characters for added security. I am allowing apostrophes, but that should be the only potential threat from the allowed characters listed in the regex below.

Once I get the regex, I'll also replace on paste using the same code block.

This is my javascript method that I reverted back to.

function CleanSentenceInput(AlphaNumString) {
    input = AlphaNumString;
    var CleanInput = input.replace(/[^a-z0-9\s.,;:'()-]/gi, '');
    CleanInput = myTrim(CleanInput);
    return CleanInput;

}

Is there a way to allow any number of line breaks by modifying this replace regex?

Test Input:

aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb

cccccccccccccccc


cccccccccccccccc

Test Result:

aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccc

Expected Result:

aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbb

cccccccccccccccc


cccccccccccccccc

Update ** It turns out that the trim function I was using was removing the line-breaks. Here is that function:

Bad trim function:

function myTrim(x) {
    return x.replace(/^\s+|\s+$/gm, '');
}

Is there a way to fix this regex so that it still replaces whitespace before and after, but not inside of the content?

Updated **

Good Trim Function:

function myTrim(x) {
    return x.replace(/^\s+|\s+$/g, '');
}
Colin P
  • 45
  • 6
  • Where in it? because I've tried it and it still results in the exact same test result. – Colin P Aug 27 '15 at 13:37
  • 2
    I believe the problem is with `myTrim`. [The regex does not cause any issues.](https://regex101.com/r/hG0qZ0/1) – Wiktor Stribiżew Aug 27 '15 at 13:38
  • I will post the myTrim function to show what it does, I don't think it removes whitespace except for beginning and end, but I'm willing to try removing to test. Give me a second. – Colin P Aug 27 '15 at 13:40
  • @stribizhev you are absolutely right. It fixed the problem. Hmm... I guess that explains why I tried everything. Thank you. I'll add the trim function, do you think theres a way to still remove space from beginning or end without breaking this? Give me a few minutes to get it updated. Thanks again – Colin P Aug 27 '15 at 13:43
  • @ColinP, just call `.trim()` instead of your own custom trim. [The trim() method removes whitespace from both ends of a string.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) – Daniel Aug 27 '15 at 13:45
  • @DanielCook There was actually a reason I moved to my own trim, though I can't recall what it was. Something wasn't working correctly in one of the browsers I was testing. I think IE doesn't support trim? http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie – Colin P Aug 27 '15 at 13:49
  • 1
    @stribizhev I also want to mention that the regex tester you used there is far superior to the one I was trying to use. Thanks for that as well; I'll be using that one in the future. – Colin P Aug 27 '15 at 14:30

1 Answers1

1

As I noted from the beginning the problem is not with the regex since

/[^a-z0-9\s.,;:'()-]/gi

matches characters other than whitespace (beside others in the character class).

In MyTrim you need to remove m because otherwise, $ is treated as a line end and ^ as line start anchors, and in fact you want to only trim the string from its beginning and end:

function myTrim(x) {
    return x.replace(/^\s+|\s+$/g, '');
}

It is also possible to use trim() (it is supported by all modern browsers, IE9 already should support it).

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Give me a second to test it, but I believe you are right. Thank you for your fast responses. – Colin P Aug 27 '15 at 13:53
  • 1
    Thank you, I've marked this as the correct answer. I never even considered my trim function was responsible. I want to keep supporting IE8 for at least a little while longer. In the future I'll most likely go back to using trim(). – Colin P Aug 27 '15 at 13:59