©
本文档使用
php中文网手册 发布
转换string。
3.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回驼峰字符串。
_.camelCase('Foo Bar');// => 'fooBar'
_.camelCase('--foo-bar--');// => 'fooBar'
_.camelCase('__FOO_BAR__');// => 'fooBar'将第一个字符转换string为大写,其余转换为小写。
3.0.0
[string=''] (字符串):要大写的字符串。
(字符串):返回大写的字符串。
_.capitalize('FRED');// => 'Fred'string通过将拉丁文补充-1(https://en.wikipedia.org/wiki/Latin-1 补编(Unicode_block%29#Character_table)和拉丁字母扩充-A字母基本拉丁字母和除去组合变音符号。
3.0.0
[string=''] (字符串):去毛刺的字符串。
(字符串):返回去毛刺字符串。
_.deburr('déjà vu');// => 'deja vu'检查是否string以给定的目标字符串结束。
3.0.0
[string=''] (字符串):要检查的字符串。
[target] (字符串):要搜索的字符串。
[position=string.length] (数字):最多搜索的位置。
(boolean):返回true如果string以targetelse 结束false。
_.endsWith('abc', 'c');// => true
_.endsWith('abc', 'b');// => false
_.endsWith('abc', 'b', 2);// => true将字符“&”,“<”,“>”,“”和“'” string转换为其对应的HTML实体。
注意:没有其他字符被转义。为了逃避额外的角色,请使用像他这样的第三方库。
虽然“>”字符为了对称而被转义,但像“>”和“/”这样的字符不需要在HTML中转义,除非它们是标记或未加引号属性值的一部分,否则没有特殊含义。有关更多详细信息,请参阅Mathias Bynens的文章 (在“半相关趣味事实”下)。
使用HTML时,应始终引用属性值以减少XSS向量。
0.1.0
[string=''] (字符串):要转义的字符串。
(字符串):返回转义字符串。
_.escape('fred, barney, & pebbles');// => 'fred, barney, & pebbles'转义RegExp特殊字符“^”,“$”,“”,“。”,“*”,“+”,“?”,“(”,“)”,“”,“”,“{”,“ }“和”|“ 中string。
3.0.0
[string=''] (字符串):要转义的字符串。
(字符串):返回转义字符串。
_.escapeRegExp('[lodash](https://lodash.com/)');// => '\[lodash\]\(https://lodash\.com/\)'转换string为Kebab的情况。
3.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回烤肉串字符串。
_.kebabCase('Foo Bar');// => 'foo-bar'
_.kebabCase('fooBar');// => 'foo-bar'
_.kebabCase('__FOO_BAR__');// => 'foo-bar'string以空格分隔的字词转换为小写字母。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回下方的字符串。
_.lowerCase('--Foo-Bar--');// => 'foo bar'
_.lowerCase('fooBar');// => 'foo bar'
_.lowerCase('__FOO_BAR__');// => 'foo bar'将第一个字符转换string为小写。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回转换后的字符串。
_.lowerFirst('Fred');// => 'fred'
_.lowerFirst('FRED');// => 'fRED'string如果它短于左侧和右侧垫length。如果填充字符不能被平均分割,则会被截断length。
3.0.0
[string=''] (字符串):要填充的字符串。
[length=0] (数字):填充长度。
[chars=' '] (字符串):用作填充的字符串。
(字符串):返回填充的字符串。
_.pad('abc', 8);// => ' abc '
_.pad('abc', 8, '_-');// => '_-abc_-_'
_.pad('abc', 3);// => 'abc'垫string右侧,如果它比短length。填充字符如果超过,则会被截断length。
4.0.0
[string=''] (字符串):要填充的字符串。
[length=0] (数字):填充长度。
[chars=' '] (字符串):用作填充的字符串。
(string): Returns the padded string.
_.padEnd('abc', 6);// => 'abc '
_.padEnd('abc', 6, '_-');// => 'abc_-_'
_.padEnd('abc', 3);// => 'abc'垫string左侧,如果是比较短的length。填充字符如果超过,则会被截断length。
4.0.0
[string=''] (字符串):要填充的字符串。
[length=0] (数字):填充长度。
[chars=' '] (字符串):用作填充的字符串。
(字符串):返回填充的字符串。
_.padStart('abc', 6);// => ' abc'
_.padStart('abc', 6, '_-');// => '_-_abc'
_.padStart('abc', 3);// => 'abc'转换string为指定基数的整数。如果radix是undefined或 0,一个radix的10使用,除非value是一个十六进制,在这种情况下radix的16使用。
注意:此方法与ES5的实现一致parseInt。
1.1.0
string (字符串):要转换的字符串。
[radix=10] (数字):解释的基数value。
(数字):返回转换后的整数。
_.parseInt('08');// => 8
_.map(['6', '08', '10'], _.parseInt);// => [6, 8, 10]重复给定的字符串n时间。
3.0.0
[string=''] (字符串):要重复的字符串。
[n=1] (数字):重复字符串的次数。
(字符串):返回重复的字符串。
_.repeat('*', 3);// => '***'
_.repeat('abc', 2);// => 'abcabc'
_.repeat('abc', 0);// => ''替换比赛为pattern在string与 replacement。
注意:此方法基于String#replace。
4.0.0
[string=''] (字符串):要修改的字符串。
pattern (RegExp | string):要替换的模式。
replacement (功能|字符串):匹配替换。
(字符串):返回修改后的字符串。
_.replace('Hi Fred', 'Fred', 'Barney');// => 'Hi Barney'转换string为蛇的情况。
3.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回蛇字符串。
_.snakeCase('Foo Bar');// => 'foo_bar'
_.snakeCase('fooBar');// => 'foo_bar'
_.snakeCase('--FOO-BAR--');// => 'foo_bar'拆分string的separator。
注意:此方法基于String#split。
4.0.0
[string=''] (string): The string to split.
separator (RegExp|string): The separator pattern to split by.
[limit] (number): The length to truncate results to.
(数组):返回字符串段。
_.split('a-b-c', '-', 2);// => ['a', 'b']转换string为启动大小写。
3.1.0
[string=''] (字符串):要转换的字符串。
(字符串):返回开始的套用字符串。
_.startCase('--foo-bar--');// => 'Foo Bar'
_.startCase('fooBar');// => 'Foo Bar'
_.startCase('__FOO_BAR__');// => 'FOO BAR'检查是否string从给定的目标字符串开始。
3.0.0
[string=''] (字符串):要检查的字符串。
[target] (字符串):要搜索的字符串。
[position=0] (数字):从中搜索的位置。
(boolean):返回true如果string以targetelse 开始false。
_.startsWith('abc', 'a');// => true
_.startsWith('abc', 'b');// => false
_.startsWith('abc', 'b', 1);// => true创建一个编译的模板函数,可以在“插入”分隔符中插入数据属性,在“转义”分隔符中插入HTML转义插值数据属性,并在“评估”分隔符中执行JavaScript。数据属性可以作为模板中的自由变量来访问。如果给定设置对象,则优先于 _.templateSettings值。
注意:在开发版本中,_.template利用sourceURL来更容易地进行调试。
有关预编译模板的更多信息,请参阅lodash的自定义构建文档。
有关Chrome扩展程序沙箱的更多信息,请参阅Chrome的扩展程序文档。
0.1.0
[string=''] (string): The template string.
[options={}] (Object): The options object.
[options.escape=_.templateSettings.escape] (RegExp): The HTML "escape" delimiter.
[options.evaluate=_.templateSettings.evaluate] (RegExp): The "evaluate" delimiter.
[options.imports=_.templateSettings.imports] (Object): An object to import into the template as free variables.
[options.interpolate=_.templateSettings.interpolate] (RegExp): The "interpolate" delimiter.
[options.sourceURL='lodash.templateSources[n]'] (string): The sourceURL of the compiled template.
[options.variable='obj'] (string): The data object variable name.
(功能):返回已编译的模板功能。
// Use the "interpolate" delimiter to create a compiled template.var compiled = _.template('hello <%= user %>!');compiled({ 'user': 'fred' });// => 'hello fred!' // Use the HTML "escape" delimiter to escape data property values.var compiled = _.template('<b><%- value %></b>');compiled({ 'value': '<script>' });// => '<b>&lt;script&gt;</b>' // Use the "evaluate" delimiter to execute JavaScript and generate HTML.var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');compiled({ 'users': ['fred', 'barney'] });// => '<li>fred</li><li>barney</li>' // Use the internal `print` function in "evaluate" delimiters.var compiled = _.template('<% print("hello " + user); %>!');compiled({ 'user': 'barney' });// => 'hello barney!' // Use the ES template literal delimiter as an "interpolate" delimiter.// Disable support by replacing the "interpolate" delimiter.var compiled = _.template('hello ${ user }!');compiled({ 'user': 'pebbles' });// => 'hello pebbles!' // Use backslashes to treat delimiters as plain text.var compiled = _.template('<%= "\\<%- value %\\>" %>');compiled({ 'value': 'ignored' });// => '<%- value %>' // Use the `imports` option to import `jQuery` as `jq`.var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';var compiled = _.template(text, { 'imports': { 'jq': jQuery } });compiled({ 'users': ['fred', 'barney'] });// => '<li>fred</li><li>barney</li>' // Use the `sourceURL` option to specify a custom sourceURL for the template.var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });compiled(data);// => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector. // Use the `variable` option to ensure a with-statement isn't used in the compiled template.var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });compiled.source;// => function(data) {// var __t, __p = '';// __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';// return __p;// } // Use custom template delimiters._.templateSettings.interpolate = /{{([\s\S]+?)}}/g;var compiled = _.template('hello {{ user }}!');compiled({ 'user': 'mustache' });// => 'hello mustache!' // Use the `source` property to inline compiled templates for meaningful// line numbers in error messages and stack traces.fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\ var JST = {\ "main": ' + _.template(mainText).source + '\ };\
');string整体而言,转换为小写字母,就像String#toLowerCase一样。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回下方的字符串。
_.toLower('--Foo-Bar--');// => '--foo-bar--'
_.toLower('fooBar');// => 'foobar'
_.toLower('__FOO_BAR__');// => '__foo_bar__'string整体转换为大写字母,就像String#toUpperCase一样。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回上方的字符串。
_.toUpper('--foo-bar--');// => '--FOO-BAR--'
_.toUpper('fooBar');// => 'FOOBAR'
_.toUpper('__foo_bar__');// => '__FOO_BAR__'从中删除前导和尾部的空格或指定的字符string。
3.0.0
[string=''] (字符串):要修剪的字符串。
[chars=whitespace] (字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trim(' abc ');// => 'abc'
_.trim('-_-abc-_-', '_-');// => 'abc'
_.map([' foo ', ' bar '], _.trim);// => ['foo', 'bar']从中删除尾部空白或指定的字符string。
4.0.0
[string=''] (字符串):要修剪的字符串。
[chars=whitespace] (字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trimEnd(' abc ');// => ' abc'
_.trimEnd('-_-abc-_-', '_-');// => '-_-abc'从中删除前导空格或指定的字符string。
4.0.0
[string=''] (字符串):要修剪的字符串。
[chars=whitespace] (字符串):要修剪的字符。
(字符串):返回修剪后的字符串。
_.trimStart(' abc ');// => 'abc '
_.trimStart('-_-abc-_-', '_-');// => 'abc-_-'string如果它长于给定的最大字符串长度,则截断。截断字符串的最后一个字符被替换为缺省为“...”的省略字符串。
4.0.0
[string=''] (字符串):要截断的字符串。
[options={}] (对象):选项对象。
[options.length=30] (数字):最大字符串长度。
[options.omission='...'] (字符串):省略表示文本的字符串。
[options.separator] (RegExp | string):要截断的分隔符模式。
(字符串):返回截断的字符串。
_.truncate('hi-diddly-ho there, neighborino');// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': ' '});// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': /,? +/});// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', { 'omission': ' [...]'});// => 'hi-diddly-ho there, neig [...]'相反的_.escape; 这种方法的HTML实体转换 &amp;,&lt;,&gt;,&quot;,和 &#39;在string其对应的字符。
注意:没有其他HTML实体未转义。为了避免额外的HTML实体使用像他这样的第三方库。
0.6.0
[string=''] (字符串):unescape的字符串。
(字符串):返回未转义的字符串。
_.unescape('fred, barney, & pebbles');// => 'fred, barney, & pebbles'string以空格分隔的字词转换为大写字母。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回上方的字符串。
_.upperCase('--foo-bar');// => 'FOO BAR'
_.upperCase('fooBar');// => 'FOO BAR'
_.upperCase('__foo_bar__');// => 'FOO BAR'将第一个字符转换string为大写。
4.0.0
[string=''] (字符串):要转换的字符串。
(字符串):返回转换后的字符串。
_.upperFirst('fred');// => 'Fred'
_.upperFirst('FRED');// => 'FRED'拆分string成它的单词的数组。
3.0.0
[string=''] (字符串):要检查的字符串。
[pattern] (RegExp | string):匹配单词的模式。
(数组):返回单词string。
_.words('fred, barney, & pebbles');// => ['fred', 'barney', 'pebbles']
_.words('fred, barney, & pebbles', /[^, ]+/g);// => ['fred', 'barney', '&', 'pebbles']