当前位置: 移动技术网 > IT编程>开发语言>正则 > 正则表达式的使用 ASP

正则表达式的使用 ASP

2017年12月12日  | 移动技术网IT编程  | 我要评论

入团申请书500字,淘宝双十二销售额,vr视频教程

复制代码 代码如下:

<%
' --------------------------------------------------------------
' match 对象

' 匹配搜索的结果是存放在 match 对象中,提供了对正则表达式匹配的只读属性的访问。
' match 对象只能通过 regexp 对象的 execute 方法来创建,该方法实际上返回了 match 对象的集合。
' 所有的 match 对象属性都是只读的。在执行正则表达式时,可能产生零个或多个 match 对象。
' 每个 match 对象提供了被正则表达式搜索找到的字符串的访问、字符串的长度,以及找到匹配的索引位置等。
' ○    firstindex 属性,返回在搜索字符串中匹配的位置。firstindex属性使用从零起算的偏移量,该偏移量是相对于搜索字符串的起始位置而言的。换言之,字符串中的第一个字符被标识为字符 0
' ○    length 属性,返回在字符串搜索中找到的匹配的长度。
' ○    value 属性,返回在一个搜索字符串中找到的匹配的值或文本。
' --------------------------------------------------------------
' response.write regexpexecute("[ij]s.", "is1 js2 is3 is4")
function regexpexecute(patrn, strng)
    dim regex, match, matches            '建立变量。
    set  regex = new regexp                '建立正则表达式。
    regex.pattern = patrn                '设置模式。
    regex.ignorecase = true                '设置是否不区分字符大小写。
    regex.global = true                    '设置全局可用性。
    set  matches = regex.execute(strng)    '执行搜索。
    for each match in matches            '遍历匹配集合。
        retstr = retstr & "match found at position "
        retstr = retstr & match.firstindex & ". match value is '"
        retstr = retstr & match.value & "'." & "<br>"
    next
    regexpexecute = retstr
end function

' --------------------------------------------------------------------
' replace 方法
' 替换在正则表达式查找中找到的文本。
' --------------------------------------------------------------------
' response.write regexpreplace("fox", "cat") & "<br>"        ' 将 'fox' 替换为 'cat'。
' response.write regexpreplace("(s+)(s+)(s+)", "$3$2$1")    ' 交换词对.
function regexpreplace(patrn, replstr)
    dim regex, str1                    ' 建立变量。
    str1 = "the quick brown fox jumped over the lazy dog."
    set  regex = new regexp            ' 建立正则表达式。
    regex.pattern = patrn            ' 设置模式。
    regex.ignorecase = true            ' 设置是否不区分大小写。
    regexpreplace = regex.replace(str1, replstr)    ' 作替换。
end function

' --------------------------------------------------------------------
' 使用 test 方法进行搜索。
' 对指定的字符串执行一个正则表达式搜索,并返回一个 boolean 值
' 指示是否找到匹配的模式。正则表达式搜索的实际模式是通过 regexp 对象的 pattern 属性来设置的。
' regexp.global 属性对 test 方法没有影响。
' 如果找到了匹配的模式,test 方法返回 true;否则返回 false
' --------------------------------------------------------------------
' response.write regexptest("功能", "重要功能")
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
%>


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网