Javascript 检测中文字符集的正则表达式
// 校验中文字符
alert(/[\u4E00-\u9FA5]+/.test('中文'));
// 校验中文字符
alert(/[\u4E00-\u9FA5]+/.test('中文'));
发表者
俞 伟军
位置在:
9/24/2007 02:36:00 PM
0
评论
标签: Character , Expression , JavaScript , Regular , Set , 中文
在Ruby/Perl/PHP中:
. (period) [^\n] 匹配除换行符(\n)之外所有字符
在Javascript中:
. (period) [^\n\r] Any character except new line and carriage return
在Ruby/Javascript中:
可以用m这个选项使得.period匹配换行符\n,不过不匹配Javascript的\r。
在PHP/Perl中:
可以用s这个选项使得.period匹配换行符\n。在PHP/Perl中的m选项是使得正则表达式里的^和$能匹配字符串的多行。
发表者
俞 伟军
位置在:
9/21/2007 10:46:00 PM
0
评论
标签: Expression , JavaScript , Period , Perl , PHP , Regular , Ruby
不要将正则表达式和 shell 中的文件名匹配模式,globs 混淆了。通常 glob 是指,在 Unix shell 下输入*.pm 将匹配所有结尾为.pm 的文件名,globs 有时也被称作模式。但严重的问题是,某些面向初级用户的书籍(可能是菜鸟写得)将 globs 叫做“正则表达式”,这绝对是错误的。
Ruby中Dir Class 中有个方法[],说明为:Equivalent to calling Dir.glob(glob_pattern, 0),而在Dir.glob方法中则有二种用法:
Dir.glob( glob_pattern,
发表者
俞 伟军
位置在:
9/21/2007 10:35:00 PM
0
评论
标签: Expression , glob , Perl , Regular , Ruby
<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br />\n");
}
var pp = /^\d(?=\.\d+)/; // pattern positive lookahead
doc('3.14'.match(pp));
doc('3.'.match(pp));
doc('.14'.match(pp));
var pn = /^\d(?!\.\d+)/; // pattern negative lookahead
doc('3'.match(pn));
doc('3.14'.match(pn));
doc('.14'.match(pn));
</script>
发表者
俞 伟军
位置在:
7/14/2007 04:16:00 PM
0
评论
标签: Expression , JavaScript , RegExp , Regular
<script type="text/javascript" charset="utf-8">
var p = new RegExp("http", "i");
document.write(p.test("Http://www.google.com"));
var customer = "Alan Turing 555-1212";
var pattern = /(\w+) \w+ ([\d-]{8})/;
document.write("<br/>" + pattern.test(customer));
if (pattern.test(customer))
{
document.write("<br/>Groups Results:");
document.write(RegExp.$1 + " " + RegExp.$2);
}
var cat = new RegExp("cat", "im");
var inputStr = "where is the white cat and the blank cat?";
var rs = cat.exec(inputStr);
document.write("<br/><br/>rs.length = " + rs.length);
document.write("<br/>rs.index = " + rs.index);
document.write("<br/>cat.global = " + cat.global);
document.write("<br/>cat.multiline = " + cat.multiline);
document.write("<br/>cat.ignoreCase = " + cat.ignoreCase);
document.write("<br/>cat.source = " + cat.source);
document.write("<br/>cat.lastIndex = " + cat.lastIndex);
document.write("<br/>rs.input = " + rs.input);
document.write("<br/>");
document.write("<br/>RegExp['$_'] = " + RegExp['$_']);
document.write("<br/>RegExp['$&] = " + RegExp['$&']);
document.write("<br/>rs = " + rs);
var lucky = "The lucky numbers are 3, 14, and 27";
var pattern = /\d+/;
document.writeln("<br/>" + "Without global we get:");
document.writeln("<br/>" + pattern.exec(lucky));
document.writeln("<br/>" + pattern.exec(lucky));
document.writeln("<br/>" + pattern.exec(lucky));
pattern = /\d+/g;
document.writeln("<br/>" + "With global we get:");
document.writeln("<br/>" + pattern.exec(lucky));
document.writeln("<br/>" + pattern.exec(lucky));
document.writeln("<br/>" + pattern.exec(lucky));
</script>
发表者
俞 伟军
位置在:
7/14/2007 09:34:00 AM
0
评论
标签: Exec , Expression , JavaScript , Regular , Test
Similarly, both or and || evaluate to true if either operand is true. They evaluate their
second operand only if the first is false. As with and, the only difference between or
and || is their precedence.
Just to make life interesting, and and or have the same precedence, and && has a higher
precedence than ||.
So how do the $-variables fit in? Well, after every pattern match, Ruby stores a refer-
ence to the result (nil or a MatchData object) in a thread-local variable (accessible
using $~). All the other regular expression variables are then derived from this object.
Although we can’t really think of a use for the following code, it demonstrates that all
the other MatchData-related $-variables are indeed slaved off the value in $~.
re = /(\d+):(\d+)/
md1 = re.match("Time: 12:34am")
md2 = re.match("Time: 10:30pm")
[ $1, $2 ] # last successful match → ["10", "30"]
$~ = md1
[ $1, $2 ] # previous successful match → ["12", "34"]
发表者
俞 伟军
位置在:
6/27/2007 11:38:00 PM
0
评论
Regular expressions are used with the RegExp methods test and exec and with the String methods match, replace, search, and split.These methods are explained in detail in the Core JavaScript Reference.
Methods that use regular expressions
Method:Description
exec:A RegExp method that executes a search for a match in a string. It returns an array of information.
test:A RegExp method that tests for a match in a string. It returns true or false.
match:A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
search:A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replace:A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split:A String method that uses a regular expression or a fixed string to break a string into an array of substrings.
When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or match methods. If you use exec or match and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp. If the match fails, the exec method returns null (which converts to false).
In the following example, the script uses the exec method to find a match in a string.
<script LANGUAGE="JavaScript1.2">
myRe=/d(b+)d/g;
myArray = myRe.exec("cdbbdbsbz");
</script>
If you do not need to access the properties of the regular expression, an alternative way of creating myArray is with this script:
<script LANGUAGE="JavaScript1.2">
myArray = /d(b+)d/g.exec("cdbbdbsbz");
</script>
If you want to construct the regular expression from a string, yet another alternative is this script:
<script LANGUAGE="JavaScript1.2">
myRe= new RegExp ("d(b+)d", "g");
myArray = myRe.exec("cdbbdbsbz");
</script>
With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.
Results of regular expression execution.
Object | Property or index | Description | In this example |
myArray | The matched string and all remembered substrings. | ["dbbd", "bb"] | |
index | The 0-based index of the match in the input string. | 1 | |
input | The original string. | "cdbbdbsbz" | |
[0] | The last matched characters. | "dbbd" | |
myRe | lastIndex | The index at which to start the next match. (This property is set only if the regular expression uses the g option, described in Executing a Global Search, Ignoring Case, and Considering Multiline Input.) | 5 |
source | The text of the pattern.Updated at the time that the regular expression is created, not executed. | "d(b+)d" |
发表者
俞 伟军
位置在:
3/17/2007 05:41:00 PM
0
评论
标签: Expression , JavaScript , Regular