underscore
underscore() -> string
将一个 Camel 形式的字符串转换为以下划线("_")分隔的一系列单词。
样例
'borderBottomWidth'.underscore();
// -> 'border_bottom_width'
注意
为将一个 DOM style 属性转换为它等价的 CSS 属性,请联合使用使用 String#dasherize 和 String#underscore 方法。
'borderBottomWidth'.underscore().dasherize();
// -> 'border-bottom-width'
...
truncate
truncate([length = 30[, suffix = '...']]) -> string
将字符串截短为指定的长度(注意:这里的长度包含了后缀部分), 并添加一个后缀(表示它仅只是一个摘要)。
当然,如果字符串的长度小于或等于指定的长度,String#truncate 将不会对字符串做任何修改。
如果未指定参数,长度默认为 30,并且后缀为 "..."。
注意,如果追加了后缀,String#truncate 将后缀也计算在结果值的长度中, 以严格地限制字符串的最终长度等于指定的长度。
样例
'A random sentence whose length exceeds 30 characters.'.truncate();
// -> 'A random sentence whose len...'
'Some random text'.truncate();
// -&...
toQueryParams
toQueryParams([separator = '&']) -> Object
解析一个 URI 查询字符串,并返回由“参数/值”对组成的对象。
这个方法真正的目的是解析查询字符串(因此参数 separator 的默认值为 "&")。
基于上述原因,它 并不 考虑问号(问号表示一个查询字符串的开始)之前的部分以及 "#" 号之后的部分,并且会在每一个“参数/值”对上调用 decodeURIComponent() 方法。
译注:在 URL 中,"#" 号一般用来表示锚点,置于最后,如 http://xxx/News/Content.aspx?id=100#first ,在 JavaScript 中,可以通过方法 location.hash 获取 URL 中 &quo...
toJSON [1.5.1]
toJSON() -> String
返回一个 JSON 格式的字符串。
样例
'The "Quoted" chronicles'.toJSON();
//-> '"The "Quoted" chronicles"'
toArray
toArray() -> [character...]
将字符串拆分为字符数组。
样例
'a'.toArray();
// -> ['a']
'hello world!'.toArray();
// -> ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
times [1.5.1]
times(count) -> string
将字符串重复 count 次。
样例
"echo ".times(3); //-> "echo echo echo "
succ
succ() -> string
用于 ObjectRange 内部。根据 Unicode 字母表转换字符串最后的字符为后续的字符。
样例
'a'.succ(); // -> 'b'
'aaaa'.succ(); // -> 'aaab'
sub
sub(pattern, replacement[, count = 1]) -> string
将字符串中前 count 个与 pattern 指定的模式匹配的子串用 replacement 替换掉。replacement 可以是一个普通的字符串、一个回调函数或是一个 Template 字符串。pattern 可以是一个字符串或是一个正则表达式。
与 String#gsub 不同,String#sub 还具有可选的第三个参数, 用于指定模式匹配的次数,如果不指定,默认为 1。
除此以外,String#sub 与 String#gsub 具有相同的功能。完整的描述请参见 String#gsub。
样例
var fruits = 'apple pear orange';
fruits.sub(' ', ', ');
// -> 'apple, pear orange'
fruits.sub(' ', ', ', 1);
// -> 'apple...
stripTags
stripTags() -> string
移除字符串中所有的 HTML 标签。
当心字符串中的 <script> 标签,因为 String#stripTags 不会 移除它们的内容。如果要完全移除 <script>(包括其内容),请使用 String#stripScripts。
样例
'a <a href="#">link</a>'.stripTags();
// -> 'a link'
'a <a href="#">link</a><script>alert("hello world!")</script>'.stripTags();
// -> 'a linkalert("hello world!")'
'a <a href="#">link</a><script>alert("hello world!")</...
stripScripts
stripScripts() -> string
移除字符串中所有的 HTML script 块。
样例
'a <a href="#">link</a><script>alert("hello world!")</script>'.stripScripts();
// -> 'a <a href="#">link</a>'
strip
strip() -> string
移除字符串首尾的所有空白符。
样例
' hello world! '.strip();
// -> 'hello world!'
startsWith 【1.5.1】
startsWith(substring) -> Boolean
检查字符串是否以 substring 作为开头。
样例
'Prototype JavaScript'.startsWith('Pro');
//-> true
scan
scan(pattern, iterator) -> string
该方法允许遍历字符串中与参数 pattern 指定的模式(可以是一个字符串或是一个正则表达式) 匹配的所有子串。返回原始字符串本身。
该方法只是简单的将参数 pattern 和 iterator 传递给 String#gsub 方法,并调用它。
样例
'apple, pear & orange'.scan(/w+/, alert);
// -> 'apple pear orange'(并连续弹出对话框显示 'apple'、'pear' 和 'orange')
可以用来填充一个数组:
var fruits = [];
'apple, pear & orange'.scan(/w+/, function(match){
fruits.push(match[0])
});
fruits.inspect()
// -> ['apple', 'pear', 'orange']
甚至可用于 DOM
'fa...
evalJSON [1.5.1]
evalJSON([sanitize = false]) -> object
执行一个 JSON 格式的字符串,并返回结果对象。如果可选的参数 sanitize 被设置为 true,则会检测字符串中是否包含恶意或错误代码,若检测到,则会停止字符串的执行, 不再调用 eval。
如果 JSON 字符串格式错误或在字符串中检测到恶意代码,将会抛出一个 SyntaxError 异常。
样例
var person = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
person.name;
//-> "Violet"
person = 'grabUserPassword()'.evalJSON(true);
/...
escapeHTML
escapeHTML() -> string
将 HTML 特殊字符转换为它们的等价实体。
样例
'<div class="article">This is an article</div>'.escapeHTML();
// -> "<div class="article">This is an article</div>"
endsWith [1.5.1]
endsWith(substring) -> Boolean
检查字符串是否以 substring 作为结尾。
样例
'slaughter'.endsWith('laughter')
// -> true