Showing posts with label Difference. Show all posts
Showing posts with label Difference. Show all posts

Tuesday, January 06, 2009

Difference of String.match and Regexp.exec

The match( ) method is the most general of the String regular-expression methods. It takes a regular expression as its only argument (or converts its argument to a regular expression by passing it to the RegExp( ) constructor) and returns an array that contains the results of the match. If the regular expression has the g flag set, the method returns an array of all matches that appear in the string. For example:

"1 plus 2 equals 3".match(/\d+/g) // returns ["1", "2", "3"]

If the regular expression does not have the g flag set, match( ) does not do a global search; it simply searches for the first match. However, match( ) returns an array even when it does not perform a global search. In this case, the first element of the array is the matching string, and any remaining elements are the parenthesized subexpressions of the regular expression. Thus, if match( ) returns an array a, a[0] contains the complete match, a[1] contains the substring that matched the first parenthesized expression, and so on. To draw a parallel with the replace( ) method, a[n] holds the contents of $n.
For example, consider parsing a URL with the following code:
var url = /(\w+):\/\/([\w.]+)\/(\S*)/;
var text = "Visit my blog at http://www.example.com/~david";
var result = text.match(url);
if (result != null) {
var fullurl = result[0]; // Contains "http://www.example.com/~david"
var protocol = result[1]; // Contains "http"
var host = result[2]; // Contains "www.example.com"
var path = result[3]; // Contains "~david"
}
Finally, you should know about one more feature of the match( ) method. The array it returns has a length property, as all arrays do. When match( ) is invoked on a nonglobal regular expression, however, the returned array also has two other properties: the index property, which contains the character position within the string at which the match begins, and the input property, which is a copy of the target string. So in the previous code, the value of the result.index property would be 17 because the matched URL begins at character position 17 in the text. The result.input property holds the same string as the text variable. For a regular expression r and string s that does not have the g flag set, calling s.match(r) returns the same value as r.exec(s). The RegExp.exec( ) method is discussed a little later in this chapter.
var pattern = /\bJava\w*\b/g;
var text = "JavaScript is more fun than Java or JavaBeans!";
var result;
while((result = pattern.exec(text)) != null) {
alert("Matched '" + result[0] +
"' at position " + result.index +
" next search begins at position " + pattern.lastIndex);
}
When exec( ) is invoked on a nonglobal pattern, it performs the search and returns the result described earlier. When regexp is a global regular expression, however, exec( ) behaves in a slightly more complex way. It begins searching string at the character position specified by the lastIndex property of regexp. When it finds a match, it sets lastIndex to the position of the first character after the match. This means that you can invoke exec( ) repeatedly in order to loop through all matches in a string. When exec( ) cannot find any more matches, it returns null and resets lastIndex to zero. If you begin searching a new string immediately after successfully finding a match in another string, you must be careful to manually reset lastIndex to zero.
Note that exec( ) always includes full details of every match in the array it returns, whether or not regexp is a global pattern. This is where exec( ) differs from String.match( ), which returns much less information when used with global patterns. Calling the exec( ) method repeatedly in a loop is the only way to obtain complete pattern-matching information for a global pattern.

This article copy from OReilly's《JavaScript The Definitive Guide》5th Edition.

Thursday, April 03, 2008

Difference of \b and [\b] in javascript

\b Match a word boundary. That is, match the position between a \w character and a \W character or between a \w character and the beginning or end of a string. (Note, however, that [\b] matches backspace.)
\B Match a position that is not a word boundary.
[\b] A literal backspace (special case).
Note that the special character-class escapes can be used within square brackets. \s matches any whitespace character, and \d matches any digit, so /[\s\d]/ matches any one whitespace character or digit. Note that there is one special case. As you'll see later, the \b escape has a special meaning. When used within a character class, however, it represents the backspace character. Thus, to represent a backspace character literally in a regular expression, use the character class with one element: /[\b]/.

Reference: Javascript the Definitive Guide 5th.

Tuesday, February 26, 2008

CSS float and document flow difference between firefox and ie7

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta http-equiv="content-type" content="text/html; charset=GBK"/>
<title>css float and document flow difference between firefox and ie7</title>
<style type="text/css">
.paragraphs {border: 1px solid #ccc; width: 300px; float: left; margin: 10px;}
.divs {border: 3px double #333; width: 600px; margin: 2px;}
</style>
</head>
<body>
text content before float elements, blah, blah.......<br />
text content before float elements, blah, blah.......
<p class="paragraphs">
text content in paragraph which is left float.
text content in paragraph which is left float.
text content in paragraph which is left float.
text content in paragraph which is left float.
text content in paragraph which is left float.
text content in paragraph which is left float.
text content in paragraph which is left float.
</p>
text content after float elements, blah, blah.......
text content after float elements, blah, blah.......
text content after float elements, blah, blah.......
text content after float elements, blah, blah.......
text content after float elements, blah, blah.......
text content after float elements, blah, blah.......
<div class="divs">
text content in normal document flow, which container start point (left top) start after content ignore the float elements(double border div element), and which content will follow float elements. IE has some different tankle with this content, this container start point will after float element.
<br />
text content in normal document flow, which container start point (left top) start after content ignore the float elements(double border div element), and which content will follow float elements. IE has some different tankle with this content, this container start point will after float element.
</div>
</body>
</html>

Monday, January 21, 2008

keywords of this in javascript methods

<html>
<head>
<title> cloures </title>
<script src="/lib/prototype/prototype.js" type="text/javascript"></script>

<script type="text/javascript">
var o = {};
var obj = {
name: 'A nice demo in obj',
fx: function() {
return this.name + " " + $A(arguments).join(', ');
},
rf: function() {
return function() { return this.name + " " + $A(arguments).join(', '); }
}
};
alert(obj.fx);
window.name = 'I am such a beautiful window!';
function runFx(f) {
return f();
}
var fx2 = obj.fx.bind(obj, 'otherArg1', 'otherArg2');
var rf2 = obj.rf.bind(obj, 'otherArg1', 'otherArg2');
alert(fx2);
document.write(runFx(obj.fx));
document.write("<br />");
document.write(runFx(fx2));
document.write("<br />");
document.write(obj.rf()());
document.write("<br />");
document.write(rf2()());
document.write("<br />");
</script>
</head>
<body>
Javascript的闭包函数(如将函数做为另一个函数/方法的参数或者是返回结果为函数)中的this会因为闭包函数使用环境不同而变化。
<br />
prototype.js里加了一个bind方法可以把this重新绑回到原对象上(如fx方法所示),但如果返回的是闭包函数,则其中的this会指向新的object(如rf2方法所示指向了window)
</body>
</html>

Javascript function and anonymous function differrence 1

Javascript函数存在于定义它们的整个作用域(包括出现在该函数语句前面的语句)内。与之相反, Javascript匿名函数只是为后续的语句定义的。如下example:


<html>
<head>
<title> test stateFunction and expressionFunction difference</title>
</head>
<body>
<div id="d1">d1</div>
<div id="d2">d2</div>
doc_write is not a function<br />
[Break on this error] doc_write(document.getElementById('d1').innerHTML);
<script type="text/javascript" charset="utf-8">
doc(document.getElementById('d1').innerHTML);
doc_write(document.getElementById('d1').innerHTML);

function doc(argument) {
document.write("<p>\n");
document.write(argument);
document.write("</p>\n");
}

var doc_write = function(args) {
document.write("<p>\n");
document.write(args);
document.write("</p>\n");
};
</script>
</body>
</html>

Monday, January 07, 2008

NOTE SYNTAX OF require/load VS. SYNTAX OF include[R4R page159]

You may have noticed that when you use require or load, you put the name of the item you’re requiring or loading in quotation marks, but with include, you don’t. require and load take strings as their arguments, whereas include takes the name of a module, in the form of a constant. The requirements to require and load are usually literal strings (in quotation marks), but a string in a variable will also work.

Friday, May 25, 2007

[转mysql 5.1 manual 2.3.16] Windows版MySQL同Unix版MySQL对比

已经证明,Windows版MySQL很稳定。Windows版MySQL的功能与相应的Unix版相同,只有以下例外:

· Windows 95和线程

Windows 95创建一个线程时大约需要200字节的主内存。MySQL的每个连接都会创建一个新线程,因此如果你的服务器正处理许多连接,你不应当在Windows 95中运行mysqld。

· 有限的端口数目

Windows系统有大约4,000个端口供客户端连接,某个端口的连接关闭后,在能够重新利用该端口前,需要2至4分钟。在客户端频繁连接并从服务器上断开时,在可以重新使用关闭的端口前,有可能用完了所有可用的端口。如果发生这种情况,MySQL服务器不会响应,即使它仍在运行。请注意机器上运行的其它应用程序也可以使用端口,此时可供MySQL使用的端口数要少。

详细信息参见http://support.microsoft.com/default.aspx?scid=kb;en-us;196271 。

· 并行读

MySQL依靠pread()和pwrite()系统调用来同时使用INSERT和SELECT。目前我们使用互斥来竞争pread()和pwrite()。我们将来想用虚拟接口替换文件级接口,因此要想更快,我们可以在NT、2000和XP上使用readfile()/writefile()接口。目前MySQL 5.1可以打开的文件的限制数目为2,048,意味着在Windows NT,2000,XP和2003上可以运行的并行线程不如Unix上多。

· 阻塞读

MySQL为每个连接使用阻塞读取,如果启用了命名管道连接,其含义如下:

o 连接不会在8小时后自动断开,而在Unix版MySQL中会发生。

o 如果连接被挂起,不杀掉MySQL则不会将其中断。

o mysqladmin kill不会杀掉睡眠中的连接。

o 只要有睡眠连接,mysqladmin shutdown不会中断。

我们计划在将来修复该问题。

· ALTER TABLE

执行ALTER TABLE语句时,将该表锁定不让其它线程使用。在Windows中,你不能删除正被另一个线程使用的文件。在将来,我们会找出办法解决该问题。

· DROP TABLE

在Windows中对一个被MERGE表应用的表执行DROP TABLE不会实现,因为MERGE处理器将表从MySQL上层映射隐藏起来。由于Windows不允许删除打开的文件,必须在删除表之前首先清空所有MERGE表(使用FLUSH TABLES)或删掉MERGE表。

· DATA DIRECTORY and INDEX DIRECTORY

在Windows中将忽略DATA DIRECTORY和INDEX DIRECTORY选项,因为Windows不支持符号连接。在具有非功能realpath()调用的系统中,这些选项也被忽略。

· DROP DATABASE

你不能删掉正被线程使用的数据库。

· 从Task Manager(任务管理器)杀掉MySQL

你不能从Task Manager(任务管理求)或使用Windows 95的shutdown工具来杀掉MySQL。你必须通过mysqladmin shutdown停止它。

· 大小写名

由于Windows对文件名大小写不敏感。因此在Windows中MySQL数据库名和表名对大小写也不敏感。唯一的限制是在同一个语句中,必须同时使用大写或小写指定数据库名和表名。请参见9.2.2节,“识别符大小写敏感性”。

· ‘\’路径名间隔符

Windows中的路径名用‘\’符间隔开,在MySQL中还是转义字符。如果你使用LOAD DATA INFILE或SELECT ... INTO OUTFILE,用‘/’符使用Unix-类文件名:

mysql> LOAD DATA INFILE 'C:/tmp/skr.txt' INTO TABLE skr;
mysql> SELECT * INTO OUTFILE 'C:/tmp/skr.txt' FROM skr;
你还可以使用双‘\’符:

mysql> LOAD DATA INFILE 'C:\\tmp\\skr.txt' INTO TABLE skr;
mysql> SELECT * INTO OUTFILE 'C:\\tmp\\skr.txt' FROM skr;
· 管道问题。

管道不能在Windows命令行提示符下可靠地工作。如果管道包括字符^Z/CHAR(24),Windows认为遇到了文件末尾并中断程序。

这主要是按照如下所示使用二进制日志的主要问题:

C:\>mysqlbinlog binary-log-name | mysql --user=root
如果使用日志时出现问题,怀疑是由于^Z / CHAR(24)字符,你可以使用下面的程序:

C:\> mysqlbinlog binary-log-file --result-file=/tmp/bin.sql
C:\> mysql --user=root --execute "source /tmp/bin.sql"
后面的命令还可以用来可靠读取任何包含二进制数据的SQL文件。

· Access denied for user错误

如果你试图运行MySQL客户端程序来连接同一机器上运行的服务器,但是遇到错误Access denied for user 'some-user'@'unknown' to database 'mysql',这意味着MySQL不能正确识别你的主机名。

要解决该问题,你应当创建一个名为\windows\hosts包含下面信息的文件:

127.0.0.1 localhost