Deal with Literals and Escape Sequences in JavaScript

Literals are special characters for which their original meaning, as a character, had been replaced by their processing software. The processing software used these characters to flag something else. In JavaScript, literals are:

. | * ? + ( ) { } [ ] ^ $ \

As a result, if one wants to deal with these literals, one would find s/he will not be able to process them normally. At lease, escape “\” would be needed to replace in front of the literal.

Escape sequences are actually aliases for single characters which do have special meaning. For instance, \n is the alias of a character with code number 0x0A, which means a newline in text. In comparison with literals, it even has worse situation because normal work processing, such as Windows Notepad, does not even show them.
In JavaScript, it is so strange JavaScript does not even have an integrated policy to deal with them. Following are examples:

Literal Asterisk, or *

For method .search(), neither .search(‘*’) nor .search(‘\*’) world work in many browsers.
For method .replace(), .replace(‘*’, newPattern) does not work. However, .replace(‘\*’ , newPattern) does work.
For method .replace(RegExp), neither .replace(new RegExp(‘*’, flag), newPattern) nor .replace(new RegExp(‘\*’, flag), newPattern) works.

Escape Sequence Newline, or \n

For method .search(), none of .search(‘\n’), .search(‘\\n’) or .search(‘\\\n’) world work.
For method .replace(), .replace(‘‘\n’’, newPattern) does work. However, .replace(‘\\n’ , newPattern) or .replace(‘\\\n’ , newPattern) does not work.
For method .replace(RegExp), .replace(new RegExp(‘\n’, flag), newPattern) does work but .replace(new RegExp(‘\\n’, flag), newPattern) and .replace(new RegExp(‘\\\n’, flag), newPattern) does not work.

As a conclusion:

1. Escape sequence, such as \n is just a presentation. Rather, it is INDEED a single character. Any attempt to place an escape ‘\’ in front of it means a failure. The correct usage is just ‘\n’.

2. Method .search() does not work at all. Forget this method at all if you are not sure if your text contains literals, or if you want your function to process escape sequences.

3. Do deal with literals, the best approach is to replace them with another non-literal character or a string first before the dealt. Sure, the best approach to replace them is to use method .replace() without the utilization of Regular Expression. Since .replace() can only deal with the first occurrence of the matching, one would need to do it recursively or by loop.

4. For escape sequences, the best approach is the same as that for literals, by replacing them with a non- escape sequence character or a string first. Surprisingly, /n and /t does not behave same with RegExp, though they are same escape sequences.

5. Universally, following coding might be the best approach for replacing both literals and escape sequences:

; for (var i=0; i‹ textContent.length; i++) textContent = textContent.replace('\*', newPattern)

or

; for (var i=0; i‹ textContent.length; i++) textContent = textContent.replace('\t', newPattern)

No comments:

Post a Comment

Labels