26 lines
1023 B
Plaintext
26 lines
1023 B
Plaintext
== Why is this an issue?
|
|
|
|
Entries in the ASCII table below code 32 are known as control characters or non-printing characters. As they are not common in JavaScript strings, using these invisible characters in regular expressions is most likely a mistake.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
const pattern1 = /\x1a/; // Noncompliant: 1a (23 base 10) is less than 32
|
|
const pattern2 = new RegExp('\x1a'); // Noncompliant: 1a (23 base 10) is less than 32
|
|
----
|
|
|
|
Instead, one should only match printable characters in regular expressions.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
const pattern1 = /\x20/;
|
|
const pattern2 = new RegExp('\x20');
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Wikipedia - https://en.wikipedia.org/wiki/ASCII[ASCII]
|
|
* Wikipedia - https://en.wikipedia.org/wiki/C0_and_C1_control_codes#C0_controls[C0 and C1 control codes]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes[Character classes]
|