3

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?

PeterWong
  • 15,951
  • 9
  • 59
  • 68
  • have you tried matching the `input` with `regexpNotWorking` one more time.. – Anirudha Jan 14 '13 at 11:11
  • @Some1.Kill.The.DJ do you mean doing line 4 twice? I just did it and still all returning null. – PeterWong Jan 14 '13 at 11:17
  • the regex is working fine in [c#](http://regexhero.net/tester/)..need to look into how `match` method is implemented in `flex`.. – Anirudha Jan 14 '13 at 11:25
  • I think I came across another question that also uses Unicode escape character in the regex in Flex and failed. I guess this might be a problem in Flex. – nhahtdh Jan 14 '13 at 12:05
  • 1
    Perhaps this is a dumb question: why check for null at the beginning of the String? Actionscript does not use null terminated Strings, as in C programming. Checking that a String is not null nor the empty String can be as simple as: `if (input)` Note that the empty string evaluates to false. – Sunil D. Jan 14 '13 at 16:54
  • @SunilD. the example was simplified so that we could focus on the \u0000 instead of the whole complex regex. My intention was to ensure some range of characters occurs at least once, which the range excludes the controls from `\u0000-\u0019`. – PeterWong Jan 15 '13 at 01:39
  • Funny, this works with ASCII range `/[^\x00-\x20]/`. Also, see [this Kirupa thread](http://www.kirupa.com/forum/showthread.php?346635-ASCII-and-Unicode-NULL-quot-0x00-quot-completely-cuts-off-end-of-string). – inhan Jan 17 '13 at 02:30

1 Answers1

1

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
}
Justin Mclean
  • 1,615
  • 13
  • 14
  • Good idea. But I think the unicode must be larger than 9999 (at least I can think of \uFFFF)...... – PeterWong Jan 17 '13 at 03:24
  • Yep should be FFFF not 9999, I'll fix. – Justin Mclean Jan 17 '13 at 03:26
  • And I should of mentioned that \u0000 not working is a bug in Flash. See issue with search above but for some reason replace can find and replace \u0000 with something. Odd to say the least. – Justin Mclean Jan 17 '13 at 03:31
  • Why flash has so many bugs....... *sigh* I wanted to make sure the string contains no any from \u0000-\u0020 (i.e. all are \u0021-\uFFFF). How could this be implemented in regexp? A regexp version should be better for my use case. – PeterWong Jan 17 '13 at 04:01
  • var regexp:RegExp = /[^\u0021-\uFFFF]/; if (string.seach(regexp) >= 0) { ... } – Justin Mclean Jan 17 '13 at 04:26