1

I'm trying to make sure that a string does not have any weird ASCII characters.

I'm trying to use character classes and negation.

 var tester =/[^\x00-\x001F\x007\x080-\xA1]+/i;

So: no ASCII characters between 00-1F, 07; or 80-A1 should be present. Everything else should be fine.

I am coming back to regular expressions after a long time away... The regular expression is NOT working. I want a string like "hello" to pass and a string like "†ack!" to fail. Or, is my JavaScript/jQuery code wrong?

The code:

var tester2 = /^[^\x00-\x1f\x80-\xa1]+$/;
    $('#testButton').click(function(){
        var text1 = $('#ackInput').val();
        console.log("text: " + text1);
        var allowed  = tester2.test(text1);
        var feedback = "allowed?" + allowed;
        console.log(feedback);
        $('#errorTestInputAllowedChars').text(feedback);
    });

An entry on jsFiddle is at http://jsfiddle.net/jillrenee42/WE79e/2/.

8
  • edited to make it clearer Commented Dec 11, 2013 at 15:39
  • @hwnd or /^[ -~]+$/ Commented Dec 11, 2013 at 15:40
  • international printed chars are desired, hence the /^[ -~]+$/ does not fill business requirements Commented Dec 11, 2013 at 15:57
  • 1
    The "dagger" symbol that you get from † is not 86 hex; it's a Unicode character (†) Commented Dec 11, 2013 at 16:16
  • 1
    I looked it up using this reference. I don't have any idea why the HTML entities were set up that way. The characters in the upper part of 8-bit encodings are kind-of not well-standardized I think; real "ASCII" is only a 7-bit code. Commented Dec 11, 2013 at 16:21

3 Answers 3

1

You need to make sure that the whole string matches:

var tester = /^[^\x00-\x001F\x007\x080-\xA1]+$/i;

That \x notation doesn't seem correct to me, and this works when I try it:

var tester = /^[^\u0000-\u001F\u0080-\u00A1]+$/i;
Sign up to request clarification or add additional context in comments.

8 Comments

I tried that. then the string "hello" is false (not true as it should be)
I updated the fiddle; jsfiddle.net/jillrenee42/WE79e/1 and it now passes EVERYTHING; including the †ack string
@JillRenee you did not update the fiddle correctly; your regular expression is still invalid, and it doesn't include the ^ and $ at the start and end.
how are you testing it? this is very frustrating; so far nothing is working for me.
@JillRenee The "dagger" symbol has a character code that falls far outside the ranges you are disallowing.
|
1

In Javascript hexcodes are 2 digit codes so following will work for you:

/^[^\x00-\x1F\x07\x80-\xFF]+$/

Javascript Regex Reference

1 Comment

Yes with \u it will take 4 digit \uhhhh form
0

Your hex encodings of your desired ranges are incorrect. You want this instead

[^\x00-\x1f\x80-\xa1]

Note I left out \x07 because that's already in the range of \x00-\x1f


EDIT

As Pointy points out, you will need to negate the entire string.

1 Comment

still not working. is it a problem with my javascript?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.