substr
專案 | 描述 |
文法 | substr(s, i, j)。
|
說明 | 字串截取操作。 |
參數 | |
傳回值 | 返回s的子字串s[i, j]。 |
樣本 | 判斷檔案類型是否為.m3u8的兩種方法: 方法一: if eq(substr($uri, -5, -1), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
方法二: uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
|
concat
專案 | 描述 |
文法 | concat(s1, ...)。
|
說明 | 字串串連操作。 |
參數 | 一個或多個參數,參數類型允許為數字字串。 |
傳回值 | 將多個參數串連為一個字串,並返回該字串。 |
樣本 | 判斷檔案類型是否為.m3u8的兩種方法: 方法一: if eq(substr($uri, -5, -1), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
方法二: uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
|
format
專案 | 描述 |
文法 | format(fmt, ···)。
|
說明 | 返回不定數量參數的格式化版本,格式化字串為第一個參數(必須是一個字串)。格式化字串遵循ISO C函數sprintf的規則。 fmt規則的格式為:%[指定參數][標識符][寬度][精度]指示符。
%% 印出百分比符號,不轉換。
%c 整數轉成對應的ASCII字元。
%d 整數轉成十進位。
%f 浮點數。
%o 整數轉成八進位。
%s 格式化成字串。
%x 整數轉成小寫十六進位。
%X 整數轉成大寫十六進位。
|
參數 | fmt:String類型,格式化字串。 可變參數:任意類型。
|
傳回值 | String類型。 |
樣本 | say(concat('format:', format('%%%s$%.2s$%s$%c$%d$%2.2f$%.2o$%x$%X', 'format', 3.1415926, true, 95, 3.1415926, 3.1415926, 3.1415926, 10, 10)))
輸出: format:%format$3.$true$_$3$3.14$03$a$A
|
upper
專案 | 描述 |
文法 | upper(s)。
|
說明 | 將字串中所有的小寫字母轉換成大寫字母。 |
參數 | s:目標字串。 |
傳回值 | 返回大寫的字串s。 |
樣本 | mystr = 'Hello, AScript'
say(upper(mystr))
輸出: HELLO, ASCRIPT
|
lower
專案 | 描述 |
文法 | lower(s)。
|
說明 | 將字串中所有的大寫字母轉換成小寫字母。 |
參數 | s:目標字串。 |
傳回值 | 返回小寫字串s。 |
樣本 | mystr = 'Hello, AScript'
say(lower(mystr))
輸出: hello, ascript
|
len
專案 | 描述 |
文法 | len(s)。
|
說明 | 擷取字串的長度。 |
參數 | s:目標字串。 |
傳回值 | 返回s的長度,整型。 |
樣本 | say(len('hello'))
輸出: 5
|
byte
專案 | 描述 |
文法 | byte(c)。
|
說明 | 擷取字元的ASCII碼。 |
參數 | c:目標字元,必須為單個字元。 |
傳回值 | 返回對應的ASCII碼,數字類型。 |
樣本 | say(byte('a'))
say(byte('A'))
輸出: 97
65
|
match_re
專案 | 描述 |
文法 | match_re(s, p, [o])。
|
說明 | 使用PCRE正則引擎,進行正則匹配,判斷字串是否匹配對應的Regex。更多資訊,請參見PCRE正則文法。 |
參數 | s:目標字串,字元類型。 p:Regex,字元類型。 o:正則引擎參數,字元類型,可選填。
|
傳回值 | 匹配成功返回true,否則返回false。 |
樣本 | url = concat('http://', $host, $uri)
m1 = match_re(url, 'http://.*\.dslex\.com/.*')
m2 = match_re(url, '^http://.*\.alibaba\.com\.cn/.*\.d\\.html(\?.*)?$')
m3 = match_re(url, '^http://.*.test.dslex.com/.*\.d\.html(\?.*)?$')
m4 = match_re(url, '^http://.*\.alibaba\.com\.cn/zt_d/')
m5 = match_re(url, '^http://tech.alibaba.com.cn/zt_d/we2015/?$')
m6 = match_re($args, 'from=wap1$')
m7 = match_re($args, 'from=comos1$')
if and(m1, or(m2, m3), not(m4), not(m5), or(not(m6), not(m7))) {
add_rsp_header('USER-DEFINED-1', 'hit1')
add_rsp_header('USER-DEFINED-2', 'hit2')
}
|
capture_re
專案 | 描述 |
文法 | capture_re(s, p, [init])。
|
說明 | 正則捕獲,並返回捕獲結果。使用PCRE正則引擎,更多資訊,請參見PCRE正則文法。 |
參數 | s:目標字串,字元類型。 p:Regex,字元類型。 init:指定匹配開始位置,下標從1開始,整型。
|
傳回值 | 匹配成功的若干子串通過字典類型返回,匹配失敗返回空字典。 |
樣本 | pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)([^?]+)\?(.*)')
sec1 = get(pcs, 1)
sec2 = get(pcs, 2)
sec3 = get(pcs, 3)
if or(not(sec1), not(sec2), not(sec3)) {
add_rsp_header('X-TENGINE-ERROR', 'auth failed - missing necessary uri set')
exit(403)
}
digest = md5(concat(sec1, sec3))
if ne(digest, sec2) {
add_rsp_header('X-TENGINE-ERROR', 'auth failed - invalid digest')
exit(403)
}
|
gsub_re
專案 | 描述 |
文法 | gsub_re(subject, regex, replace, [option])。
|
說明 | 正則替換,並返回替換後的副本。使用PCRE正則引擎,詳細資料,請參見PCRE正則文法。 |
參數 | subject:目標字串,字元類型。 regex:Regex,字元類型。 replace:替換字元,字元類型。 replace部分可以引用匹配部分,即:
$0:表示regex整體匹配的部分。 $N:表示regex第N個()匹配的部分。
option:正則引擎參數,字元類型,可選。
|
傳回值 | subject中所有的符合參數regex的子串都將被參數replace所指定的字串所替換,並返回替換後的副本。
|
樣本 | subject = 'Hello,Es'
regex = '([a-zA-Z])[a-z]+'
replace = '[$0,$1]'
add_rsp_header('X-DEBUG-GSUB-RE', gsub_re(subject, regex, replace))
輸出: X-DEBUG-GSUB-RE: [Hello,H], [Es,E]
|
split
專案 | 描述 |
文法 | split(s, [sep])。
|
說明 | 分隔字串,並返回分隔結果。 |
參數 | |
傳回值 | 分隔元素包含在字典類型中返回,由數字下標作key,起始下標為1,例如:[1]=xx, [2]=yy;若sep為空白,則預設以任意空白字元分隔。預設空白字元包含:空格、Tab。 |
樣本 | if $arg_from {
t = split($arg_from, ',')
#get()函數擷取字典t中第1個值
if get(t, 1) {
say(concat('[1]=', get(t, 1)))
}
#get()函數擷取字典t中第2個值
if get(t, 2) {
say(concat('[2]=', get(t, 2)))
}
#get()函數擷取字典t中第3個值
if get(t, 3) {
say(concat('[3]=', get(t, 3)))
}
}
響應: [1]=xx1
[2]=xx2
[3]=xx3
|
split_as_key
專案 | 描述 |
文法 | split_as_key(s, [sep])。
|
說明 | 分隔字串,並返回分隔結果。 |
參數 | s:目標字串,字元類型。 sep:分隔字元,字元類型。
|
傳回值 | 同split(),區別在於key:[分割元素]->[分割元素]。 |
樣本 | def echo_each(k, v, u) {
#get函數擷取字典中的值
s = concat(k, '=', v, ' u=', get(u, 1))
say(s)
}
if $arg_from {
t = split_as_key($arg_from, ',')
#foreach函數遍曆字典中的元素
foreach(t, echo_each, ['hi,ascript'])
}
響應: xx2=xx2 u=hi,ascript
xx1=xx1 u=hi,ascript
xx3=xx3 u=hi,ascript
|
tohex
專案 | 描述 |
文法 | tohex(s)。
|
說明 | 十六進位轉換。 |
參數 | s:字串。 |
傳回值 | 返回s的十六進位可讀形式。 |
樣本 | digest = 'xxxx'
add_rsp_header('X-AScript-TOHEX', tohex(digest))
輸出: X-AScript-TOHEX: 78787878
|
tobin
專案 | 描述 |
文法 | tobin(str)。
|
說明 | 16進位轉ASCII字串。 |
參數 | str: 每個字元由兩個16進位數字組成的字串,不區分大小寫。 |
傳回值 | String類型。 |
樣本 | say(concat('tobin:', tobin('2F2F')))
輸出: tobin://
|
tostring
專案 | 描述 |
文法 | tostring(a)。
|
說明 | 字串類型轉換。 |
參數 | a:任意類型。 |
傳回值 | 返回參數a轉換後的字串。 |
樣本 | s = tostring(123)
add_rsp_header('X-DSL-TOSTRING', s)
輸出: X-DSL-TOSTRING: 123
|
tochar
專案 | 描述 |
文法 | tochar(n1, n2, ...)。
|
說明 | |
參數 | nX:整型參數。 |
傳回值 | 返迴轉換後的字串。 |
樣本 | say(tochar(97))
say(tochar(97,98))
|
reverse
專案 | 描述 |
文法 | reverse(str)。
|
說明 | 字串反轉。 |
參數 | str:待反轉的字串。 |
傳回值 | 返回字串類型,返回反轉後的字串。 |
樣本 | say(reverse('hello'))
輸出: olleh
|
find
專案 | 描述 |
文法 | string.find (s, substr, [pos])。
|
說明 | 在目標字串中搜尋指定的字串。 |
參數 | |
傳回值 | 成功:返回數群組類型。 索引1,返回尋找的起始位置。 索引2,返回尋找的截止位置。
失敗:返回空數組。
|
樣本 | str = 'hello dsl'
#add_rsp_header函數添加回應標頭string-find()-start,tostring函數轉換為字串,預期輸出7
add_rsp_header('string-find()-start', tostring(get(find(str, 'dsl'), 1)))
str = 'hello dsl 12'
#add_rsp_header函數添加回應標頭string-find()-end,tostring函數轉換為字串,預期輸出9
add_rsp_header('string-find()-end', tostring(get(find(str, 'dsl'), 2)))
str = 'hello dsl'
#add_rsp_header函數添加回應標頭string-find()-tail-start,tostring函數轉換為字串,預期輸出7
add_rsp_header('string-find()-tail-start', tostring(get(find(str, 'dsl', -6), 1)))
str = 'hello dsl 12'
#add_rsp_header函數添加回應標頭string-find()-tail-start,tostring函數轉換為字串,預期輸出9
add_rsp_header('string-find()-tail-end', tostring(get(find(str, 'dsl', -6), 2)))
輸出: string-find()-start:7
string-find()-end:9
string-find()-tail-start:7
string-find()-tail-end:9
|
trim
專案 | 描述 |
文法 | trim(s, [loc])。
|
說明 | 剔除s兩端或指定端的全部空白字元,並返回剔除後的字串。 |
參數 | |
傳回值 | 返回剔除後的字串。 |
樣本 | say(concat('trim():', trim(' abcd ')))
say(concat('trim(left):', trim(' abcd ', 'left')))
say(concat('trim(right):', trim(' abcd ', 'right')))
輸出: trim():abcd
trim(left):abcd
trim(right): abcd
|