Showing posts with label .replace(). Show all posts
Showing posts with label .replace(). Show all posts

Removal of New Line Break and Tab/Indentation in JavaScript

Here is the best possible coding for removal of newline break and tab/indentation:

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

This approach avoids the possible non-working of method .search() and RegExp. For details please refer to http://koncordpartners.blogspot.com/2010/02/deal-with-literals-and-escape-sequences.html

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)

The JavaScript RegExp Object

RegExp provides a vehicle for other JavaScript methods to conduct a batch process. For instance .replace(“oldSubStr”, “newSubStr”) would only replace the first matching pattern. By using RegExp, it can be archived for all matching patterns: .replace(new RegExp(“oldSubStr”, “flag”), “newSubStr”), or simply .replace(/oldSubStr/flag, “newSubStr”). Three flags g, i, m can be used as parameter for search: Global Search, Ignore Case and Multiline Input.

There are two problems with RegExp, however.

1. The simple way of RegExp can’t deal with string reference, such as .replace(/oldSubStr/flag, “newSubStr”). oldSubStr must be a string, not the string reference.

2. If your text, as a string data type, contains following literals, it won’t work properly:

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

It is thought when escape sign \ added it would work. Unfortunately, in reality, it usually won’t work. It is therefore suggested when those literals exist in your text, you should write your own recursive or loop function to process it. Here is an example:

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

http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.araxis.com/merge/topic_regexpreference.html
http://www.regular-expressions.info/reference.html
http://www.evolt.org/regexp_in_javascript

Labels