快速入门
概览
变量(Variables)
混合(Mixins)
嵌套(Nesting)
运算(Operations)
转义(Escaping)
函数(Functions)
命名空间和访问符
映射(Maps)
作用域(Scope)
注释(Comments)
导入(Importing)
Less.js 用法
命令行用法
浏览器使用
Less.js选项
预装插件
程序化使用
API
Contributing to Less.js
Less 函数手册
逻辑函数
字符串函数
列表函数
数学函数
类型函数
其他功能
颜色定义函数
颜色通道函数
颜色操作函数
颜色混合函数
进阶指南
合并
父选择器
扩展
变量
Mixins
CSS Guards
Detached Rulesets
@import At-Rules
@plugin At-Rules
Maps (NEW!)
Less入门文档
网站首页
字符串函数
### escape Applies URL-encoding to special characters found in the input string. - These characters are not encoded: `,`, `/`, `?`, `@`, `&`, `+`, `'`, `~`, `!` and `$`. - Most common encoded characters are: `\
`, `#`, `^`, `(`, `)`, `{`, `}`, `|`, `:`, `>`, `<`, `;`, `]`, `[` and `=`. Parameters: `string`: a string to escape. Returns: escaped string content without quotes. Example: ```less escape('a=1') ``` Output: ```less a%3D1 ``` **Note:** if the parameter is not a string, output is not defined. The current implementation returns undefined on color and unchanged input on any other kind of argument. This behavior should not be relied on and may change in the future. ### e #### String escaping. It expects string as a parameter and return its content as is, but without quotes. It can be used to output CSS value which is either not valid CSS syntax, or uses proprietary syntax which Less doesn't recognize. Parameters: string - a string to escape. Returns: string - the escaped string, without quotes. Example: ```less @mscode: "ms:alwaysHasItsOwnSyntax.For.Stuff()" filter: e(@mscode); ``` Output: ```less filter: ms:alwaysHasItsOwnSyntax.For.Stuff(); ``` ### % format The function `%(string, arguments ...)` formats a string. The first argument is string with placeholders. All placeholders start with percentage symbol `%` followed by letter `s`,`S`,`d`,`D`,`a`, or `A`. Remaining arguments contain expressions to replace placeholders. If you need to print the percentage symbol, escape it by another percentage `%%`. Use uppercase placeholders if you need to escape special characters into their utf-8 escape codes. The function escapes all special characters except `()'~!`. Space is encoded as `%20`. Lowercase placeholders leave special characters as they are. Placeholders: - `d`, `D`, `a`, `A` - can be replaced by any kind of argument (color, number, escaped value, expression, ...). If you use them in combination with string, the whole string will be used - including its quotes. However, the quotes are placed into the string as they are, they are not escaped by "/" nor anything similar. - `s`, `S` - can be replaced by any expression. If you use it with string, only the string value is used - quotes are omitted. - Parameters: - `string`: format string with placeholders, - `anything`* : values to replace placeholders. Returns: formatted string. Example: ```less format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less"); format-a-d-upper: %('repetitions: %A file: %D', 1 + 2, "directory/file.less"); format-s: %("repetitions: %s file: %s", 1 + 2, "directory/file.less"); format-s-upper: %('repetitions: %S file: %S', 1 + 2, "directory/file.less"); ``` Output: ```less format-a-d: "repetitions: 3 file: "directory/file.less""; format-a-d-upper: "repetitions: 3 file: %22directory%2Ffile.less%22"; format-s: "repetitions: 3 file: directory/file.less"; format-s-upper: "repetitions: 3 file: directory%2Ffile.less"; ``` ### replace #### Replaces a text within a string. Released v1.7.0 Parameters: - `string`: The string to search and replace in. - `pattern`: A string or regular expression pattern to search for. - `replacement`: The string to replace the matched pattern with. - `flags`: (Optional) regular expression flags. Returns: a string with the replaced values. Example: ```less replace("Hello, Mars?", "Mars\?", "Earth!"); replace("One + one = 4", "one", "2", "gi"); replace('This is a string.', "(string)\.$", "new $1."); replace(~"bar-1", '1', '2'); ``` Result: ```less "Hello, Earth!"; "2 + 2 = 4"; 'This is a new string.'; bar-2; ```
上一篇:
逻辑函数
下一篇:
列表函数