escape
将 URL-encoding 应用于在输入字符串中找到的特殊字符。
- 这些字符未编码:
,
、/
、?
、@
、&
、+
、'
、~
、!
和$
。 - 最常见的编码字符是:
\<space\>
、#
、^
、(
、)
、{
、}
、|
、:
、>
、<
、;
、]
、[
和=
。
参数: string
: 要转义的字符串。
返回: 不带引号转义 string
内容。
例子:
escape('a=1')
输出:
a%3D1
注意: 如果参数不是字符串,则未定义输出。 当前的实现在颜色上返回 undefined
,在任何其他类型的参数上返回不变的输入。 不应依赖此行为,并且将来可能会更改。
e
字符串转义。
它期望字符串作为参数并按原样返回其内容,但不带引号。 它可用于输出无效 CSS 语法或使用 Less 无法识别的专有语法的 CSS 值。
参数: string
- 要转义的字符串。
返回: string
- 转义字符串,不带引号。
例子:
@mscode: "ms:alwaysHasItsOwnSyntax.For.Stuff()"
filter: e(@mscode);
输出:
filter: ms:alwaysHasItsOwnSyntax.For.Stuff();
% 格式
函数
%(string, arguments ...)
格式化一个字符串。
第一个参数是带占位符的字符串。 所有占位符都以百分比符号 %
开头,后跟字母 s
、S
、d
、D
、a
或 A
。 其余参数包含用于替换占位符的表达式。 如果你需要打印百分比符号,请使用另一个百分比 %%
将其转义。
如果你需要将特殊字符转义为它们的 utf-8 转义码,请使用大写占位符。
该函数转义除 ()'~!
以外的所有特殊字符。 空格编码为 %20
。 小写占位符保留特殊字符。
占位符:
d
,D
,a
,A
- 可以替换为任何类型的参数(颜色、数字、转义值、表达式...)。 如果将它们与字符串结合使用,将使用整个字符串 - 包括它的引号。 但是,引号按原样放置在字符串中,它们不会被 "/" 或类似的东西转义。s
,S
- 可以用任何表达式替换。 如果将它与字符串一起使用,则仅使用字符串值 - 省略引号。
参数:
string
: 带占位符的格式字符串,anything
* : 替换占位符的值。
返回: 格式化 string
。
例子:
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");
输出:
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
替换字符串中的文本。
发布于 v1.7.0
参数:
string
: 要搜索和替换的字符串。pattern
: 要搜索的字符串或正则表达式模式。replacement
: 用于替换匹配模式的字符串。flags
: (可选)正则表达式标志。
返回: 具有替换值的字符串。
例子:
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');
结果:
"Hello, Earth!";
"2 + 2 = 4";
'This is a new string.';
bar-2;