Search code examples
regexapache-flexflex4.5

matching not \u0000 in Flex


Not sure why but the behaviour is strange. Whenever I use the \u0000 in the regular expression, it then matches nothing.

var regexpNotWorking:RegExp = new RegExp("[^\u0000-\u0020]");
var regexpWorking:RegExp = new RegExp("[^\u0001-\u0020]");
var input:String = "I should be valid";
trace("not working: " + input.match(regexpNotWorking));
trace("working: " + input.match(regexpWorking));

the output are:

not working: null
working: I

Anyone has idea why \u0001 is working, but \u0000 is not?

How could I make sure the input does not contain \u0000?


Solution

  • Why not remove the negation and change the regular expression to this:

    var regexp:RegExp = /[\u0021-\uFFFF]/;
    

    BTW no need to use new RegExp or quotes around regular expressions in Flex.

    But that's probably not quite the answer you are looking for. Here's one way to find if a string contains a NUL character.

    public function stringHasNul(value:String):Boolean {
        return (value.length != value.replace("\u0000", "NUL").length);
    }
    
    var hasNul:String = "ABC\u0000DEF"
    var noNuls:String = "ABCDEF";
    
    trace(hasNul.length); // should be 7
    trace(hasNul.search("\u0000")); // wrongly gives 0, should be 3
    trace(noNuls.length); // should be 6
    trace(noNuls.search("\u0000"));  // wrongly gives 0, should be -1
    
    trace(stringHasNul(hasNul)); // displays true
    trace(stringHasNul(noNuls)); // display false
    

    To check if any characters are in the range \u0000 to \u0020 you can do this:

    var regexp:RegExp = /[^\u0021-\uFFFF]/;
    
    if (somestring.search(regexp) == -1)
    {
        // somestring is valid do something with it
    }