深柳堂

如狐添翼:FireFox3正则表达式搜索插件/Find Bar/

原来在FireFox2.x中使用过一个插件FindBar,十分好用。升级到firefox3.0之后,FB居然冬眠。今天终于看到更新,能继续在firefox中使用了,心情超爽,特写日志以记之。

意译一下作者的介绍。插件主页、介绍原文在这里

——————-正则表达式分割线——————

/Find Bar/

/Find Bar/ is a new extension for Firefox that is still a little bit in progress. I’ve always believed that one of the best features of Firefox is it’s quick find bar. I probably use it about 20 times a day, if not more. But it has to be said its a fairly simple beast. When it comes to more powerful searches you’re just out of luck. This extension adds a whole new dimension to the find bar, regular expressions. The regular expressions are implemented using the JavaScript engine so check the JavaScript RegExp syntax for the full details.

/Find Bar/是FireFox的新插件,目前仍在完善中。对于firefox,我一直觉得它方便快捷的搜索栏是其亮点之一。我每天使用该功能20次以上。但是必须承认,其搜索功能太过薄弱。当你要搜索更复杂的内容时,它就无能为力了。

本插件为搜索栏添加了新选项:正则表达式。它使用了JavaScript正则式引擎。请查询JavaScript RegExp文档来了解语法细节内容。

有待解决的两个问题:

  • Whitespace. HTML is made up a lot of this, most of it is ignored by the browser and not visible on the page. At the moment this extension doesn’t ignore anything so you may find there are more spaces between words than you expected. Should I err on the side of accuracy as it is now, or collapse all whitespace?空白符(水平制表符、空格等)。HTML中包含了许多空白符,其中一大部分被浏览器忽略掉,不在页面上显示出来。目前该插件没有忽略任何字符,因此你会发现,单词之间往往有更多的空白符符。我是该精确地显示出每个空白字符呢,还是该将其全部压缩?
  • Block content. The standard find bar wont find searches that span blocks (paragraphs in human terms). This extension does, which while potentially useful also causes some issues. One thought is to make each paragraph like a line then you can use line breaks to match paragraphs as you might expect.区块内容。标准的搜索栏不会跨区块(亦即段落)搜索,而本插件搜索时却会跨越区块,这既是便利之处,但是也有可能带来副作用。一种思路是将每一段落处理为一个文本行,这样您就可以使用换行符来匹配段落。

该插件的兼容性

FireFox Firefox 2.0b1 - 3.1a2pre
ThunderBird Thunderbird 2.0 - 3.0a1

下载地址

/Find Bar/ Install v1.0.1

(Installs from addons.mozilla.org)

——————-正则表达式分割线——————

跨区块搜索我觉得不是大问题,对于空白字符,我的解决方法是,只要是使用正则表达式来搜索,就在使用空格时使用\s+来表示。这样,从内容上,它表示了任何种类的空白字符;从数量上,它表示了最少一个(多则不限)的空白字符,既不错杀,也无冤死。

由此引发的感慨是:人的眼睛所能看到的字符是有限的,其所看到内容与事实的真相有时未必相符。而程序是精确的,每一个比特都明察秋毫。(例如在汇编语言中的数字0与ASCII字符’0′就截然不同。虽然在perl中,两者单独出现时被视为是同一事物。)

该插件还能结合原搜索栏的全部高亮显示选项来显示,便于显示所有的匹配。在搜索英文等外语时,你会发现这一点十分有用。

另外有一点得陇望蜀的心思是,如果该插件支持保存常用搜索就好了。比方搜索email地址的、URL的,日期的,诸如此类。虽如此,这款插件已经十分好用,强烈推荐。

匹配用户名的asp正则表达式(包括中文)

有人在正则表达式中文站贴出这样一道问题:

求ASP 用户名 表达式

用户名长度在2-20字符之间,由中文/大小写字母/数字/中划线-/下线线_组成。

这个问题不算难,只要下边一行核心代码就能搞定:

"^[-_a-zA-Z0-9\u4e00-\u9fa5]{2,20}$"

关键是没有使用过ASP语言。按此页的提示,设置了ASP环境。查询了一些在线的入门级ASP教程之后,解答如下:

 
<form action="verify.asp" method="post">
姓名:
<input name="name" type="text" />
 
<input name="Submit" type="submit" value="提交" />
<input name="Submit2" type="reset" value="重置" />
</form>

它调用以下verify.asp文件:

&lt;%
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = False ' 设置是否区分大小写。
retVal = regEx.Test(strng) ' 执行搜索测试。
If retVal Then
RegExpTest = "合法用户名。"
Else
RegExpTest = "非法用户名。"
End If
End Function
 
name=request.form("name")
psw=request.form("psw")
sex=request.form("sex")
city=request.form("city")
Response.write RegExpTest("^[-_a-zA-Z0-9\u4e00-\u9fa5]{2,20}$", name)
%&gt;

运行界面见附图。

另外,还有一些正则表达式可供参考:

匹配中文字符的正则表达式

[\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内)正则表达式

[^\x00-\xff]

匹配空行的正则表达式

\n[\s|   ]*\r

匹配HTML标记的正则表达式

/ &lt;(.*)&gt; .* &lt;\/\1&gt; | &lt;(.*)   \/&gt; /

匹配首尾空格的正则表达式

(^\s*)|(\s*$)

正则表达式限制只能输入中文:

onkeyup= "value=value.replace(/[^\u4E00-\u9FA5]/g, ' ') "   onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\u4E00-\u9FA5]/g, ' ')) "

正则表达式限制只能输入全角字符:

onkeyup= "value=value.replace(/[^\uFF00-\uFFFF]/g, ' ') "   onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\uFF00-\uFFFF]/g, ' ')) "

正则表达式限制只能输入数字:

onkeyup= "value=value.replace(/[^\d]/g, ' ')   "onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\d]/g, ' ')) "

正则表达式限制只能输入数字和英文:

onkeyup= "value=value.replace(/[\W]/g, ' ')   "onbeforepaste= "clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[^\d]/g, ' ')) "