This topic describes the syntax, features, parameters, and return values of string functions. This topic also provides examples of these functions.
substr
Item | Description |
Syntax | substr(s, i, j)
|
Description | Extracts a substring from a string. |
Parameter | s: the string from which you want to extract a substring. i: the position at which the extraction starts, counting from 1. A value of -1 specifies the rightmost character of the string. Data type: integer. j: the position at which the extraction ends, counting from 1. A value of -1 specifies the rightmost character of the string. Data type: integer.
|
Return value | A substring s[i, j] that is extracted from the source string specified by s is returned. |
Example | You can use the following methods to determine whether a file is an M3U8 file: Method 1: if eq(substr($uri, -5, -1), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
Method 2: uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
|
concat
Item | Description |
Syntax | concat(s1, ...)
|
Description | Concatenates strings. |
Parameter | The strings that you want to concatenate. You can specify one or more strings. Numeric values are supported. |
Return value | A concatenated string. |
Example | You can use the following methods to determine whether a file is an M3U8 file: Method 1: if eq(substr($uri, -5, -1), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
Method 2: uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
say(concat($uri, ' is .m3u8'))
}
|
format
Item | Description |
Syntax | format(fmt, ···)
|
Description | Formats the values of one or more parameters. The format string is the first parameter, which must specify a string. The format string follows the specification of the sprintf parameter used by functions in the ISO C programming language. The syntax of a format string specified by fmt is %[parameter][flag][field width][precision]specifier . %%: prints literal percentage signs (%).
%c: converts integers into ASCII characters.
%d: converts integers into decimal numbers.
%f: floating-point numbers.
%o: converts integers into octal numbers.
%s: formats as a string.
%x: converts integers into hexadecimal numbers in lowercase letters.
%X: converts integers into hexadecimal numbers in uppercase letters.
|
Parameter | |
Return value | A string. |
Example | 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)))
Output: format:%format$3.$true$_$3$3.14$03$a$A
|
upper
Item | Description |
Syntax | upper(s)
|
Description | Converts a string to uppercase letters. |
Parameter | s: the string that you want to convert. |
Return value | The string specified by the s parameter in uppercase letters. |
Example | mystr = 'Hello, AScript'
say(upper(mystr))
Output: HELLO, ASCRIPT
|
lower
Item | Description |
Syntax | lower(s)
|
Description | Converts a string to lowercase letters. |
Parameter | s: the string that you want to convert. |
Return value | The string specified by the s parameter in lowercase letters. |
Example | mystr = 'Hello, AScript'
say(lower(mystr))
Output: hello, ascript
|
len
Item | Description |
Syntax | len(s)
|
Description | Queries the length of a string. |
Parameter | s: the string that you want to query. |
Return value | The length of the string specified by the s parameter. Data type: integer. |
Example | say(len('hello'))
Output: 5
|
byte
Item | Description |
Syntax | byte(c)
|
Description | Queries the ASCII value of a character. |
Parameter | c: the character whose ASCII value you want to query. You can specify only one character. |
Return value | The ASCII value of the specified character. Data type: numeric. |
Example | say(byte('a'))
say(byte('A'))
Output: 97
65
|
match_re
Item | Description |
Syntax | match_re(s, p, [o])
|
Description | Uses the Perl Compatible Regular Expressions (PCRE) engine for regular expression matching. For more information, see PCRE syntax. |
Parameter | s: the string that you want to match. Data type: string. p: the regular expression for matching. Data type: string. o: the regular expression engine. Data type: string. This parameter is optional.
|
Return value | If the string matches the regular expression, true is returned. Otherwise, false is returned. |
Example | 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
Item | Description |
Syntax | capture_re(s, p, [init])
|
Description | Captures the matches of a string and returns the matching substrings. For more information about PCRE, see PCRE syntax. |
Parameter | s: the string that you want to match. Data type: string. p: the regular expression for matching. Data type: string. init: the position to start matching, counting from 1. Data type: integer.
|
Return value | If the string matches the regular expression, the matching substrings in the dictionary type are returned. Otherwise, an empty dictionary is returned. |
Example | 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
Item | Description |
Syntax | gsub_re(subject, regex, replace, [option])
|
Description | Replaces all matches of a string and returns the string after the replacement. For more information about PCRE, see PCRE syntax. |
Parameter | subject: the string that you want to match. Data type: string. regex: the regular expression. Data type: string. replace: the string for replacement. Data type: string. You can specify the replace parameter by using the matching substrings. option: the regular expression engine. Data type: string. This parameter is optional.
|
Return value | The function replaces all substrings that match the specified regex parameter in the specified subject parameter with those specified by the replace parameter and returns the string after the replacement. |
Example | subject = 'Hello, Es'
regex = '([a-zA-Z])[a-z]+'
replace = '[$0,$1]'
add_rsp_header('X-DEBUG-GSUB-RE', gsub_re(subject, regex, replace))
Output: X-DEBUG-GSUB-RE: [Hello,H], [Es,E]
|
split
Item | Description |
Syntax | split(s, [sep])
|
Description | Splits a string into an array of substrings and returns the array. |
Parameter | |
Return value | An array of key-value pairs in the dictionary type. The value of the key parameter is a number that starts from 1, for example, [1]=xx, [2]=yy. If sep is left empty, the string is split by whitespace characters. Whitespace characters include space characters and tab characters (\t). |
Example | if $arg_from {
t = split($arg_from, ',')
#The get() function retrieves the first value in the dictionaty t.
if get(t, 1) {
say(concat('[1]=', get(t, 1)))
}
#The get() function retrieves the second value in the dictionary t.
if get(t, 2) {
say(concat('[2]=', get(t, 2)))
}
#The get() function retrieves the third value in the dictionary t.
if get(t, 3) {
say(concat('[3]=', get(t, 3)))
}
}
Request: ?from=xx1,xx2,xx3
Response: [1]=xx1
[2]=xx2
[3]=xx3
|
split_as_key
Item | Description |
Syntax | split_as_key(s, [sep])
|
Description | Splits a string into an array of substrings and returns the array. |
Parameter | |
Return value | Response parameters are returned in the same way as the split() function. However, the key parameter is named after each split element: Element 1 -> Element 2 . |
Example | def echo_each(k, v, u) {
#The get function retrieves the value that correspondes to the specified key in the dictionary.
s = concat(k, '=', v, ' u=', get(u, 1))
say(s)
}
if $arg_from {
t = split_as_key($arg_from, ',')
#The foreach function traverses elements in the dictionary.
foreach(t, echo_each, ['hi,ascript'])
}
Request: ?from=xx1,xx2,xx3
Response: xx2=xx2 u=hi,ascript
xx1=xx1 u=hi,ascript
xx3=xx3 u=hi,ascript
|
tohex
Item | Description |
Syntax | tohex(s)
|
Description | Converts a string to a hexadecimal string. |
Parameter | s: the string that you want to convert. |
Return value | A hexadecimal string that is converted from the string specified by the s parameter. |
Example | digest = 'xxxx'
add_rsp_header('X-AScript-TOHEX', tohex(digest))
Output: X-AScript-TOHEX: 78787878
|
tobin
Item | Description |
Syntax | tobin(str)
|
Description | Converts a hexadecimal string to an ASCII string. |
Parameter | str: a string of hexadecimal digits, which is not case-sensitive. |
Return value | A string. |
Example | say(concat('tobin:', tobin('2F2F')))
Output: tobin://
|
tostring
Item | Description |
Syntax | tostring(a)
|
Description | Converts data of any type to a string. |
Parameter | a: the data that you want to convert. Data type: any type. |
Return value | A string that is converted from the value specified by the a parameter. |
Example | s = tostring(123)
add_rsp_header('X-DSL-TOSTRING', s)
Output: X-DSL-TOSTRING: 123
|
tochar
Item | Description |
Syntax | tochar(n1, n2, ...)
|
Description | Converts one or more internal integers to a string. For example, 48 corresponds to the character "0". The length of the returned string is the total length of all parameters after conversion.
|
Parameter | nX: the integers that you want to convert. You can specify one or more integers. |
Return value | A string that is converted from integers. |
Example | say(tochar(97))
say(tochar(97,98))
|
reverse
Item | Description |
Syntax | reverse(str)
|
Description | Reverses a string. |
Parameter | str: the string that you want to reverse. |
Return value | A string reversed from the specified string. Data type: CHAR. |
Example | say(reverse('hello'))
Output: olleh
|
find
Item | Description |
Syntax | string.find (s, substr, [pos])
|
Description | Searches for a substring in a specified string. |
Parameter | s: the string that you want to search. substr: the substring for which you want to search. pos: the position where the search starts. Data type: numeric. This parameter is optional. You can specify a negative integer. The default value is 1.
|
Return value | |
Example | str = 'hello dsl'
#The add_rsp_header function adds the string-find()-start header to the value. The tostring function converts the value to a string. The expected output is 7.
add_rsp_header('string-find()-start', tostring(get(find(str, 'dsl'), 1)))
str = 'hello dsl 12'
#The add_rsp_header function adds the string-find()-end header to the value. The tostring function converts the value to a string. The expected output is 9.
add_rsp_header('string-find()-end', tostring(get(find(str, 'dsl'), 2)))
str = 'hello dsl'
#The add_rsp_header function adds the string-find()-tail-start header to the value. The tostring function converts the value to a string. The expected output is 7.
add_rsp_header('string-find()-tail-start', tostring(get(find(str, 'dsl', -6), 1)))
str = 'hello dsl 12'
#The add_rsp_header function adds the string-find()-tail-start header to the value. The tostring function converts the value to a string. The expected output is 9.
add_rsp_header('string-find()-tail-end', tostring(get(find(str, 'dsl', -6), 2)))
Output: string-find()-start:7
string-find()-end:9
string-find()-tail-start:7
string-find()-tail-end:9
|
trim
Item | Description |
Syntax | trim(s, [loc])
|
Description | Removes all whitespace characters at the beginning or at the end of the string specified by the s parameter, and returns a string with the specified whitespace characters removed. |
Parameter | |
Return value | A string with the specified whitespace characters removed. |
Example | say(concat('trim():', trim(' abcd ')))
say(concat('trim(left):', trim(' abcd ', 'left')))
say(concat('trim(right):', trim(' abcd ', 'right')))
Output: trim():abcd
trim(left):abcd
trim(right): abcd
|