The following introduces the extension of Prototype to String object:
This part mainly adds several useful methods to the string object:
strip(): Remove the whitespace on both sides of the string, for example " jj ".strip() returns "jj"
stripTags(): Remove the whitespace in the string html tag
stripScripts(): Remove the javascript code segment in the string
extractScripts(): Return the javascript code in the string and return the array
evalScripts(): Execute the javascript code in the string
escapeHTML(): Convert the html code in the string into a format that can be directly displayed, for example, convert < to <, there is a bug in IE6. The string returned by this operation turns multiple connected blanks into one, so many line breaks and so on are removed
unescapeHTML(): Reverse process of escapeHTML
truncate(length, truncation): truncation, for example, "abcdefghigkl".truncate(10) returns abcdefg..., truncation defaults to "..." toQueryParams(separator)/parseQuery(separator): Convert a querystring into a hash table (actually an object. In javascript, the object can be used as a hash table, because the properties or methods of the object can be passed through object[propertyName ] to access)
toArray(): return this.split(''), convert into a character array
camelize(): Convert background-color to Convert to backgroundColor form and use it in style/css
capitalize(): Returns a string with the first letter capitalized
inspect(useDoubleQuotes): Returns the string Representation, such as "sdfj"sfa".inspect() returns "'sdfj"sfa'"
gsub(pattern, replacement): pattern is a regular expression, replacement is a function (or is a template string), use replacement for each part of the string that matches pattern, and then replace the original matching part with the value returned by replacement, for example, "skdjfAsfdjkAdk".gsub(/A/,function(match) {return match[0].toLowerCase()}), convert all A's in the string into a. Be careful not to add the g option in pattern, because gsub will recursively execute the match method
sub(pattern, replacement , count): Another form of gsub, but you can set the number of executions
scan(pattern, iterator): Similar to gsub, but returns the string itself, that is Says to execute the iterator for each match in pattern, but does not return the replaced string "skdjfAsfdjkAdk".gsub(/A/,function(){alert 'have a A'})
underscore(): 'borderBottomWidth'.underscore() -> 'border_bottom_width'
dasherize(): 'Hello_World'.dasherize() -> 'Hello-World'
Template template class:
Usage:
var template = new Template(replacement, pattern); pattern) replaces the property value of object