Showing posts with label RegExp. Show all posts
Showing posts with label RegExp. Show all posts

mod_rewrite Tutorial

First to check if your mod_rewrite is enabled. Check phpinfo.php if you see mod_rewrite, it is enabled. If you do not see it, it is not necessary mean your server is not enabled. You may wish to go to following address to check further: http://www.wallpaperama.com/forums/how-to-test-check-if-mod-rewrite-is-enabled-t40.html

Put it simple, mod_rewrite is just to find a matched string pattern in user’s input URL, and to replace it by the substitution string. So, the center of mod_rewrite is RewriteRule, which is responsible for the match and replace. The rest directives are just for setting purpose. The most used configuration directives other than RewriteRule are RewriteEngine, RewriteOptions.

RewriteEngine’s common value is:
On – This is because default value for this directive is off.

RewriteOptions’ common value is:
Inherit - This forces the current configuration to inherit the configuration of the parent.

RewriteRule’s syntax is:
RewriteRule Pattern Substitution [Flag]

Pattern needs to be bracketed by anchors ^youInputURL$. Pattern consists of two parts, the static substring you known already and those dynamic part. You do not need to change any for static substring, while you would need to group the dynamic part. The grouping can be done by bracket (). There are also two parts inside of (), first is the character class grouping by [], and second is the flag to tell whether the character class has only one character or nil, or more, etc. The character class is ruled by RegExp.

Following is the syntax for the pattern:
(text) - Grouping of text
[chars] - Character class: One of chars
[^chars] - Character class: None of chars
text1|text2 - Alternative: text1 or text2
? - 0 or 1 character of the preceding text
* - 0 or N character of the preceding text
. - 1 character of the preceding text
+ - 1 or N character of the preceding text
\char - escape that particular char, for instance to specify the chars ".[]()" etc.

The substitution consists of two parts, the static string and the dynamic part as grouped in preceding pattern. The second is always shown as $1.

Flag’s syntax is [SOMETHING, SOMETHING, SOMETHING]. SOMETHING is the flag. Following are common used flags:
NC - This makes the input pattern case-insensitive.
L - Stop the rewriting process here and don't apply any more rewriting rules. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules.

Example:
RewriteEngine On
RewriteOptions Inherit
RewriteRule ^([A-Za-z0-9-]+)$ /php/main.php?uname=$1 [NC,L]


http://corz.org/serv/tricks/htaccess2.php
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
http://www.workingwith.me.uk/articles/scripting/mod_rewrite

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