SlideShare a Scribd company logo
String Prototype enhances the String object with a series of useful methods for String.prototype ranging from the trivial to the complex. Tired of stripping trailing whitespaces, try our String.strip method. Want to replace replace ? Have a look at String.sub and String.gsub. Need to parse a query string ?  blank :: String blank() -> Boolean   Check if the string is 'blank', meaning either empty or containing only whitespace .   '' .blank(); //-> true   '  ' .blank(); //-> true   ' a ' .blank(); //-> false
camelize :: String camelize() -> string   Converts a string separated by dashes into a camelCase equivalent. For instance, 'foo-bar' would be converted to 'fooBar'.   Prototype uses this internally for translating CSS properties into their DOM style property equivalents.   Examples   'background-color' .camelize(); // -> 'backgroundColor'   '-moz-binding' .camelize(); // -> 'MozBinding'
capitalize :: String capitalize() -> string   Capitalizes the first letter of a string and downcases all the others.   Examples   'hello'.capitalize(); // -> 'Hello'   'HELLO WORLD!'.capitalize(); // -> 'Hello world!'
dasherize :: String dasherize() -> string   Replaces every instance of the underscore character ("_") by a dash ("-").   Example   'border_bottom_width' .dasherize(); // -> 'border-bottom-width'   Note   Used in conjunction with underscore(), dasherize() converts a DOM style into its CSS equivalent.   'borderBottomWidth' .underscore().dasherize(); // -> 'border-bottom-width'
empty :: String empty() -> Boolean   Checks if the string is empty .   '' .empty(); //-> true   '  ' .empty(); //-> false endsWith :: String endsWith(substring) -> Boolean   Checks if the string ends with substring .   'slaughter' .endsWith( 'laughter' ) // -> true
escapeHTML :: String escapeHTML() -> string  Converts HTML special characters to their entity equivalents.   Example '<div class=&quot;article&quot;>This is an article</div>' .escapeHTML();   // -> &quot;&lt;div class=&quot;article&quot;&gt;This is an article&lt;/div&gt;&quot;
evalJSON :: String evalJSON([sanitize]) -> object   Evaluates   the JSON in the string and returns the resulting object. If the optional sanitize parameter is set to true, the string is checked for possible malicious attempts and eval is not called if one is detected.   If the JSON string is not well formated or if a malicious attempt is detected a SyntaxError is thrown.   person =  '{ &quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot; }' .evalJSON(); person.name; //-> &quot;Violet&quot;   person =  'grabUserPassword()' .evalJSON( true ); //-> SyntaxError: Badly formated JSON string: 'grabUserPassword()'   Note Always set the  sanitize  parameter to   true  for data coming from externals sources to prevent XSS attacks. See also toJSON
evalScripts :: String evalScripts() -> [returnedValue...]   Evaluates the content of any script block present in the string. Returns an array containing the value returned by each script .   Examples   'lorem... <script>2 + 2</script>' .evalScripts(); // -> [4]   '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .evalScripts(); // -> [4, undefined] (and displays 'hello world!' in the alert dialog)
extractScripts :: String extractScripts() -> [script...]   Exctracts the content of any script block present in the string and returns them as an array of strings .   This method is used internally by evalScripts(). It does not evaluate the scripts (use evalScripts() to do that), but can be usefull if you need to evaluate the scripts at a later date.   Examples 'lorem... <script>2 + 2</script>' .extractScripts(); // -> ['2 + 2'] '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .extractScripts(); // -> ['2 + 2', 'alert(&quot;hello world!&quot;)'] Notes To evaluate the scripts later on, you can use the following:   var  myScripts =  '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .extractScripts(); // -> ['2 + 2', 'alert(&quot;hello world!&quot;)'] var myReturnedValues = myScripts.map( function (script) {    return   eval (script); });   // -> [4, undefined] (and displays 'hello world!' in the alert dialog)
gsub :: String gsub(pattern, replacement) -> string   Returns the string with every occurence of a given pattern replaced by either a regular string, the returned value of a function or a Template string. The pattern can be a string or a regular expression.   If its second argument is a string gsub() works just like the native JavaScript method replace() set to global match .   var mouseEvents =  'click dblclick mousedown mouseup mouseover mousemove mouseout' ;   mouseEvents.gsub( ' ' ,  ', ' ); // -> 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'   mouseEvents.gsub(/\s+/,  ', ' ); // -> 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'   If you pass it a function, it will be invoked for every occurrence of the pattern with the match of the current pattern as its unique argument.    Note that this argument is the returned value of the match() method called on the current pattern. It is in the form of an array where the first element is the entire match and every subsequent one corresponds to a parenthesis group in the regex. Cont…
gsub :: String    …Cont mouseEvents.gsub(/\w+/,  function (match){ return   'on'  + match[ 0 ].capitalize()});   // -> 'onClick onDblclick onMousedown onMouseup onMouseover onMousemove onMouseout'   var markdown =  '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)' ;   markdown.gsub(/!\[(.*?)\]\((.*?)\)/,  function (match){    return   '<img alt=&quot;'  + match[ 1 ] +  '&quot; src=&quot;'  + match[ 2 ] +  '&quot; />' ; }); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> <img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; />'   Lastly, you can pass gsub() a Template string in which you can also access the returned value of the match() method using the ruby inspired notation: #{0} for the first element of the array, #{1} for the second one, and so on. So our last example could be easily re-written as:   markdown.gsub(/!\[(.*?)\]\((.*?)\)/,  '<img alt=&quot; #{1}&quot; src=&quot;#{2}&quot; />'); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> <img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; />'   If you need an equivalent to gsub() but without global match set on, try sub().   Note   Do not use  the &quot;g&quot; flag on the regex as this will create an  infinite loop.
include :: String include(substring) -> Boolean  Check if the string contains a substring .   'Prototype framework' .include( 'frame' ); //-> true   'Prototype framework' .include( 'frameset' ); //-> false inspect :: String inspect([useDoubleQuotes = false]) -> String   Returns a debug-oriented version of the string (i.e. wrapped in single or double quotes, with backslashes and quotes escaped).   For more information on inspect methods, see Object.inspect.   Examples   'I\' m so happy. '.inspect(); // -> ' \ 'I\\\' m so happy.\ ''  (displayed  as   'I\' m so happy. ' in an alert dialog or the console)   ' I\ 'm so happy.' .inspect( true ); // -> '&quot;I'm so happy.&quot;'  (displayed as &quot;I'm so happy.&quot; in an alert dialog or the console)
parseQuery :: String Alias of  toQueryParams .   :-) scan :: String scan(pattern, iterator) -> string   Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). Returns the original string .   Internally just calls gsub() passing it pattern and iterator as arguments. Examples 'apple, pear & orange' .scan(/\w+/, alert);   // -> 'apple pear orange' (and displays 'apple', 'pear' and 'orange' in                              three successive alert dialogs)   Cont..
scan :: String …Cont Can be used to populate an array:   var fruits = []; 'apple, pear & orange' .scan(/\w+/,  function (match){ fruits.push(match[ 0 ])}); fruits.inspect() // -> ['apple', 'pear', 'orange']   or even to work on the DOM:   'failure-message, success-message & spinner' .scan(/(\w|-)+/, Element.toggle) // -> 'failure-message, success-message & spinner'                    (and toggles the visibility of each DOM element)   Note   Do not use the &quot;g&quot; flag on the regex as this will create an infinite loop.
startsWith :: String startsWith(substring) -> Boolean   Checks if the string starts with substring .   'Prototype JavaScript' .startsWith( 'Pro' ); //-> true strip :: String strip() -> string   Strips all leading and trailing whitespace from a string.   Example '    hello world!    ' .strip(); // -> 'hello world!‘ stripScripts :: String stripScripts() -> string   Strips a string of anything that looks like an HTML   script   block .   Example 'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripScripts(); // -> 'a <a href=&quot;#&quot;>link</a>'
stripTags :: String stripTags() -> string   Strips a string of any HTML tag.   Watch out for <script> tags in your string, as stripTags() will not remove their content.  Use stripScripts() to do so.   Examples   'a <a href=&quot; #&quot;>link</a>' .stripTags(); // -> 'a link'   'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripTags(); // -> 'a linkalert(&quot;hello world!&quot;)'   'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripScripts().stripTags(); // -> 'a link'   See also   String:  stripScripts
sub :: String sub(pattern, replacement[, count = 1]) -> string   Returns a string with the first  count  occurrences of  pattern  replaced by either a regular string, the returned value of a function or a  Template  string.  pattern  can be a string or a regular expression.   Unlike gsub(), sub() takes a third optional parameter which specifies the number of occurrences of the pattern which will be replaced. If not specified, it will default to 1.   Apart from that, sub() works just like gsub(). Please refer to it for a complete explanation. Cont…
Examples var fruits =  'apple pear orange' ;     fruits.sub( ' ' ,  ', ' ); // -> 'apple, pear orange'     fruits.sub( ' ' ,  ', ' ,  1 ); // -> 'apple, pear orange'     fruits.sub( ' ' ,  ', ' ,  2 ); // -> 'apple, pear, orange'   fruits.sub(/\w+/,  function (match){ return  match[ 0 ].capitalize() +  ',' },  2 ); // -> 'Apple, Pear, orange'   var markdown =  '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)' ;   markdown.sub(/!\[(.*?)\]\((.*?)\)/,  function (match){    return   '<img alt=&quot;'  + match[ 1 ] +  '&quot; src=&quot;'  + match[ 2 ] +  '&quot; />' ; }); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> ![an orange](/img/orange.jpg)'   markdown.sub(/!\[(.*?)\]\((.*?)\)/,  '<img alt=&quot; #{1}&quot; src=&quot;#{2}&quot; />'); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> ![an orange](/img/orange.jpg)'   Note Do not  use the &quot;g&quot; flag on the regex as this will create an  infinite loop .
succ :: String succ() -> string  Used internally by  ObjectRange . Converts the last character of the string to the following character in the Unicode alphabet.   Examples 'a' .succ(); // -> 'b' 'aaaa' .succ(); // -> 'aaab' times :: String times(count) -> String   Concatenates the string  count  times.   &quot;echo &quot; .times( 3 ); //-> &quot;echo echo echo &quot;
toArray :: String toArray() -> [character...]  Splits the string character-by-character and returns an array with the result.   Examples   'a' .toArray(); // -> ['a']   'hello world!' .toArray(); // -> ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'] toJSON :: String toJSON() -> String   Returns a JSON string.   'The &quot;Quoted&quot; chronicles' .toJSON(); //-> '&quot;The \&quot;Quoted\&quot; chronicles&quot;'   See also evalJSON
toQueryParams :: String toQueryParams([separator = '&']) -> Object   Parses a URI-like query string and returns an object composed of parameter/value pairs .   This method is realy targeted at parsing query strings (hence the default value of &quot;&&quot; for the separator argument).   For this reason, it does  not  consider anything that is either before a question mark (which signals the beginning of a query string) or beyond the hash symbol (&quot;#&quot;), and runs decodeURIComponent() on each parameter/value pair.   toQueryParams() also aggregates the values of identical keys into an array of values.   Note that parameters which do not have a specified value will be set to undefined. Cont…  
toQueryParams :: String    …Cont Examples   'section=blog&id=45' .toQueryParams(); // -> {section: 'blog', id: '45'}   'section=blog;id=45' .toQueryParams(); // -> {section: 'blog', id: '45'}   'http://www.example.com?section=blog&id=45#comments' .toQueryParams(); // -> {section: 'blog', id: '45'}   'section=blog&tag=javascript&tag=prototype&tag=doc' .toQueryParams(); // -> {section: 'blog', tag: ['javascript', 'prototype', 'doc']}   'tag=ruby on rails' .toQueryParams(); // -> {tag: 'ruby%20on%20rails'}   'id=45&raw' .toQueryParams(); // -> {id: '45', raw: undefined}
truncate :: String each(iterator) -> Hash   Iterates over the name/value pairs in the hash.   Of course, truncate() does not modify strings which are shorter than the specified length.   If unspecified, the length parameter defaults to 30 and the suffix to &quot;...&quot;.   Note that truncate() takes into consideration the length of the appended suffix so as to make the returned string of exactly the specified length.   Examples   'A random sentence whose length exceeds 30 characters.' .truncate(); // -> 'A random sentence whose len...'   'Some random text' .truncate(); // -> 'Some random text.'   'Some random text' .truncate( 10 ); // -> 'Some ra...'   'Some random text' .truncate( 10 ,  ' [...]' ); // -> 'Some [...]'
underscore :: String underscore() -> string   Converts a camelized string into a series of words separated by an underscore (&quot;_&quot;).   Example 'borderBottomWidth' .underscore(); // -> 'border_bottom_width'   Note Used in conjunction with dasherize(), underscore() converts a DOM style into its CSS equivalent. 'borderBottomWidth' .underscore().dasherize(); // -> 'border-bottom-width'
unescapeHTML :: String unescapeHTML() -> string   Strips tags and converts the entity forms of special HTML characters to their normal form .   Examples   'x &gt; 10' .unescapeHTML() // -> 'x > 10'   '<h1>Pride &amp; Prejudice</h1>' .unescapeHTML() // -> 'Pride & Prejudice'

More Related Content

PPT
Array
PPT
Falcon初印象
PPT
PDF
Perl.Hacks.On.Vim
PDF
RubyConf Portugal 2014 - Why ruby must go!
PDF
Programming Language Swift Overview
ODP
Abus at a glance
PDF
Perl.Hacks.On.Vim Perlchina
Array
Falcon初印象
Perl.Hacks.On.Vim
RubyConf Portugal 2014 - Why ruby must go!
Programming Language Swift Overview
Abus at a glance
Perl.Hacks.On.Vim Perlchina

What's hot (20)

ODP
Evolving Software with Moose
PPT
Cleancode
ODP
The bones of a nice Python script
PDF
Introduction to Swift programming language.
PDF
Swift Programming Language
PDF
Perl.Hacks.On.Vim Perlchina
PPT
Exploiting Php With Php
PDF
Denis Lebedev, Swift
PDF
Barely Legal Xxx Perl Presentation
PDF
Idiomatic Javascript (ES5 to ES2015+)
ODP
Introduction to Perl - Day 1
KEY
Using Regular Expressions and Staying Sane
PPT
Argon walkthru 1-26
PDF
Swift Programming Language
PDF
The hidden and new parts of JS
PDF
Abuse Perl
KEY
Good Evils In Perl (Yapc Asia)
PPTX
A Few Interesting Things in Apple's Swift Programming Language
PDF
Cocoa Design Patterns in Swift
PDF
Swift 2
Evolving Software with Moose
Cleancode
The bones of a nice Python script
Introduction to Swift programming language.
Swift Programming Language
Perl.Hacks.On.Vim Perlchina
Exploiting Php With Php
Denis Lebedev, Swift
Barely Legal Xxx Perl Presentation
Idiomatic Javascript (ES5 to ES2015+)
Introduction to Perl - Day 1
Using Regular Expressions and Staying Sane
Argon walkthru 1-26
Swift Programming Language
The hidden and new parts of JS
Abuse Perl
Good Evils In Perl (Yapc Asia)
A Few Interesting Things in Apple's Swift Programming Language
Cocoa Design Patterns in Swift
Swift 2
Ad

Viewers also liked (20)

PPT
Php Sessoins N Cookies
PPTX
Odyssey Claims
PPT
Prototype
PPT
Event
PPT
Javascript Oop
PPT
PPT
Position
PPT
Insertion
PPT
Php Calling Operators
PPT
Php Rss
PPTX
Cookies and sessions
PPTX
PHP Cookies and Sessions
PPT
Php with MYSQL Database
PPT
Cookies and sessions
PPT
Unit 4 memory system
PPTX
Cookie and session
PPT
Direct Memory Access
PPTX
8237 dma controller
PPTX
Direct Memory Access(DMA)
Php Sessoins N Cookies
Odyssey Claims
Prototype
Event
Javascript Oop
Position
Insertion
Php Calling Operators
Php Rss
Cookies and sessions
PHP Cookies and Sessions
Php with MYSQL Database
Cookies and sessions
Unit 4 memory system
Cookie and session
Direct Memory Access
8237 dma controller
Direct Memory Access(DMA)
Ad

Similar to Prototype js (20)

PPT
PPT
Perl Presentation
ODP
JavaScript and jQuery Fundamentals
PPT
CGI With Object Oriented Perl
PDF
My First Rails Plugin - Usertext
ODP
How Xslate Works
PPT
Php Using Arrays
ODP
Javascript Unit Testing
PPT
JQuery Basics
ODP
Java Boilerplate Busters
PPT
Introduction To Lamp
PPT
Testing Javascript with Jasmine
PPTX
Oscon 2010 Specs talk
ODP
Scala 2 + 2 > 4
ODP
Why Scala?
PPT
JavaScript
PPTX
Combres
PPT
Sencha Touch Intro
PDF
Lettering js
Perl Presentation
JavaScript and jQuery Fundamentals
CGI With Object Oriented Perl
My First Rails Plugin - Usertext
How Xslate Works
Php Using Arrays
Javascript Unit Testing
JQuery Basics
Java Boilerplate Busters
Introduction To Lamp
Testing Javascript with Jasmine
Oscon 2010 Specs talk
Scala 2 + 2 > 4
Why Scala?
JavaScript
Combres
Sencha Touch Intro
Lettering js

More from mussawir20 (20)

PPT
Php Operators N Controllers
PPT
Database Design Process
PPT
Php Simple Xml
PPT
Php String And Regular Expressions
PPT
Php Sq Lite
PPT
Php Reusing Code And Writing Functions
PPT
Php Oop
PPT
Php My Sql
PPT
Php File Operations
PPT
Php Error Handling
PPT
Php Crash Course
PPT
Php Basic Security
PPT
PPT
Javascript
PPT
Object Range
PPT
Prototype Utility Methods(1)
PPT
PPT
Template
PPT
Class
PPT
Element
Php Operators N Controllers
Database Design Process
Php Simple Xml
Php String And Regular Expressions
Php Sq Lite
Php Reusing Code And Writing Functions
Php Oop
Php My Sql
Php File Operations
Php Error Handling
Php Crash Course
Php Basic Security
Javascript
Object Range
Prototype Utility Methods(1)
Template
Class
Element

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf

Prototype js

  • 1. String Prototype enhances the String object with a series of useful methods for String.prototype ranging from the trivial to the complex. Tired of stripping trailing whitespaces, try our String.strip method. Want to replace replace ? Have a look at String.sub and String.gsub. Need to parse a query string ? blank :: String blank() -> Boolean   Check if the string is 'blank', meaning either empty or containing only whitespace .   '' .blank(); //-> true   '  ' .blank(); //-> true   ' a ' .blank(); //-> false
  • 2. camelize :: String camelize() -> string   Converts a string separated by dashes into a camelCase equivalent. For instance, 'foo-bar' would be converted to 'fooBar'.   Prototype uses this internally for translating CSS properties into their DOM style property equivalents.   Examples   'background-color' .camelize(); // -> 'backgroundColor'   '-moz-binding' .camelize(); // -> 'MozBinding'
  • 3. capitalize :: String capitalize() -> string   Capitalizes the first letter of a string and downcases all the others.   Examples   'hello'.capitalize(); // -> 'Hello'   'HELLO WORLD!'.capitalize(); // -> 'Hello world!'
  • 4. dasherize :: String dasherize() -> string   Replaces every instance of the underscore character (&quot;_&quot;) by a dash (&quot;-&quot;).   Example   'border_bottom_width' .dasherize(); // -> 'border-bottom-width'   Note   Used in conjunction with underscore(), dasherize() converts a DOM style into its CSS equivalent.   'borderBottomWidth' .underscore().dasherize(); // -> 'border-bottom-width'
  • 5. empty :: String empty() -> Boolean   Checks if the string is empty .   '' .empty(); //-> true   '  ' .empty(); //-> false endsWith :: String endsWith(substring) -> Boolean   Checks if the string ends with substring .   'slaughter' .endsWith( 'laughter' ) // -> true
  • 6. escapeHTML :: String escapeHTML() -> string  Converts HTML special characters to their entity equivalents.   Example '<div class=&quot;article&quot;>This is an article</div>' .escapeHTML();   // -> &quot;&lt;div class=&quot;article&quot;&gt;This is an article&lt;/div&gt;&quot;
  • 7. evalJSON :: String evalJSON([sanitize]) -> object   Evaluates the JSON in the string and returns the resulting object. If the optional sanitize parameter is set to true, the string is checked for possible malicious attempts and eval is not called if one is detected.   If the JSON string is not well formated or if a malicious attempt is detected a SyntaxError is thrown.   person =  '{ &quot;name&quot;: &quot;Violet&quot;, &quot;occupation&quot;: &quot;character&quot; }' .evalJSON(); person.name; //-> &quot;Violet&quot;   person =  'grabUserPassword()' .evalJSON( true ); //-> SyntaxError: Badly formated JSON string: 'grabUserPassword()'   Note Always set the sanitize parameter to true for data coming from externals sources to prevent XSS attacks. See also toJSON
  • 8. evalScripts :: String evalScripts() -> [returnedValue...]   Evaluates the content of any script block present in the string. Returns an array containing the value returned by each script .   Examples   'lorem... <script>2 + 2</script>' .evalScripts(); // -> [4]   '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .evalScripts(); // -> [4, undefined] (and displays 'hello world!' in the alert dialog)
  • 9. extractScripts :: String extractScripts() -> [script...]   Exctracts the content of any script block present in the string and returns them as an array of strings .   This method is used internally by evalScripts(). It does not evaluate the scripts (use evalScripts() to do that), but can be usefull if you need to evaluate the scripts at a later date.   Examples 'lorem... <script>2 + 2</script>' .extractScripts(); // -> ['2 + 2'] '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .extractScripts(); // -> ['2 + 2', 'alert(&quot;hello world!&quot;)'] Notes To evaluate the scripts later on, you can use the following:   var  myScripts =  '<script>2 + 2</script><script>alert(&quot;hello world!&quot;)</script>' .extractScripts(); // -> ['2 + 2', 'alert(&quot;hello world!&quot;)'] var myReturnedValues = myScripts.map( function (script) {    return   eval (script); });   // -> [4, undefined] (and displays 'hello world!' in the alert dialog)
  • 10. gsub :: String gsub(pattern, replacement) -> string   Returns the string with every occurence of a given pattern replaced by either a regular string, the returned value of a function or a Template string. The pattern can be a string or a regular expression.   If its second argument is a string gsub() works just like the native JavaScript method replace() set to global match .   var mouseEvents =  'click dblclick mousedown mouseup mouseover mousemove mouseout' ;   mouseEvents.gsub( ' ' ,  ', ' ); // -> 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'   mouseEvents.gsub(/\s+/,  ', ' ); // -> 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'   If you pass it a function, it will be invoked for every occurrence of the pattern with the match of the current pattern as its unique argument.   Note that this argument is the returned value of the match() method called on the current pattern. It is in the form of an array where the first element is the entire match and every subsequent one corresponds to a parenthesis group in the regex. Cont…
  • 11. gsub :: String …Cont mouseEvents.gsub(/\w+/,  function (match){ return   'on'  + match[ 0 ].capitalize()});   // -> 'onClick onDblclick onMousedown onMouseup onMouseover onMousemove onMouseout'   var markdown =  '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)' ;   markdown.gsub(/!\[(.*?)\]\((.*?)\)/,  function (match){    return   '<img alt=&quot;'  + match[ 1 ] +  '&quot; src=&quot;'  + match[ 2 ] +  '&quot; />' ; }); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> <img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; />'   Lastly, you can pass gsub() a Template string in which you can also access the returned value of the match() method using the ruby inspired notation: #{0} for the first element of the array, #{1} for the second one, and so on. So our last example could be easily re-written as:   markdown.gsub(/!\[(.*?)\]\((.*?)\)/,  '<img alt=&quot; #{1}&quot; src=&quot;#{2}&quot; />'); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> <img alt=&quot;an orange&quot; src=&quot;/img/orange.jpg&quot; />'   If you need an equivalent to gsub() but without global match set on, try sub().   Note   Do not use the &quot;g&quot; flag on the regex as this will create an infinite loop.
  • 12. include :: String include(substring) -> Boolean  Check if the string contains a substring .   'Prototype framework' .include( 'frame' ); //-> true   'Prototype framework' .include( 'frameset' ); //-> false inspect :: String inspect([useDoubleQuotes = false]) -> String   Returns a debug-oriented version of the string (i.e. wrapped in single or double quotes, with backslashes and quotes escaped).   For more information on inspect methods, see Object.inspect.   Examples   'I\' m so happy. '.inspect(); // -> ' \ 'I\\\' m so happy.\ ''  (displayed  as   'I\' m so happy. ' in an alert dialog or the console)   ' I\ 'm so happy.' .inspect( true ); // -> '&quot;I'm so happy.&quot;'  (displayed as &quot;I'm so happy.&quot; in an alert dialog or the console)
  • 13. parseQuery :: String Alias of toQueryParams .   :-) scan :: String scan(pattern, iterator) -> string   Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). Returns the original string .   Internally just calls gsub() passing it pattern and iterator as arguments. Examples 'apple, pear & orange' .scan(/\w+/, alert);   // -> 'apple pear orange' (and displays 'apple', 'pear' and 'orange' in                              three successive alert dialogs)   Cont..
  • 14. scan :: String …Cont Can be used to populate an array:   var fruits = []; 'apple, pear & orange' .scan(/\w+/,  function (match){ fruits.push(match[ 0 ])}); fruits.inspect() // -> ['apple', 'pear', 'orange']   or even to work on the DOM:   'failure-message, success-message & spinner' .scan(/(\w|-)+/, Element.toggle) // -> 'failure-message, success-message & spinner'                    (and toggles the visibility of each DOM element)   Note   Do not use the &quot;g&quot; flag on the regex as this will create an infinite loop.
  • 15. startsWith :: String startsWith(substring) -> Boolean   Checks if the string starts with substring .   'Prototype JavaScript' .startsWith( 'Pro' ); //-> true strip :: String strip() -> string   Strips all leading and trailing whitespace from a string.   Example '    hello world!    ' .strip(); // -> 'hello world!‘ stripScripts :: String stripScripts() -> string   Strips a string of anything that looks like an HTML script block .   Example 'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripScripts(); // -> 'a <a href=&quot;#&quot;>link</a>'
  • 16. stripTags :: String stripTags() -> string   Strips a string of any HTML tag.   Watch out for <script> tags in your string, as stripTags() will not remove their content. Use stripScripts() to do so.   Examples   'a <a href=&quot; #&quot;>link</a>' .stripTags(); // -> 'a link'   'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripTags(); // -> 'a linkalert(&quot;hello world!&quot;)'   'a <a href=&quot; #&quot;>link</a><script>alert(&quot;hello world!&quot;)</script>' .stripScripts().stripTags(); // -> 'a link'   See also   String: stripScripts
  • 17. sub :: String sub(pattern, replacement[, count = 1]) -> string   Returns a string with the first count occurrences of pattern replaced by either a regular string, the returned value of a function or a Template string. pattern can be a string or a regular expression.   Unlike gsub(), sub() takes a third optional parameter which specifies the number of occurrences of the pattern which will be replaced. If not specified, it will default to 1.   Apart from that, sub() works just like gsub(). Please refer to it for a complete explanation. Cont…
  • 18. Examples var fruits =  'apple pear orange' ;     fruits.sub( ' ' ,  ', ' ); // -> 'apple, pear orange'     fruits.sub( ' ' ,  ', ' ,  1 ); // -> 'apple, pear orange'     fruits.sub( ' ' ,  ', ' ,  2 ); // -> 'apple, pear, orange'   fruits.sub(/\w+/,  function (match){ return  match[ 0 ].capitalize() +  ',' },  2 ); // -> 'Apple, Pear, orange'   var markdown =  '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)' ;   markdown.sub(/!\[(.*?)\]\((.*?)\)/,  function (match){    return   '<img alt=&quot;'  + match[ 1 ] +  '&quot; src=&quot;'  + match[ 2 ] +  '&quot; />' ; }); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> ![an orange](/img/orange.jpg)'   markdown.sub(/!\[(.*?)\]\((.*?)\)/,  '<img alt=&quot; #{1}&quot; src=&quot;#{2}&quot; />'); // -> '<img alt=&quot;a pear&quot; src=&quot;/img/pear.jpg&quot; /> ![an orange](/img/orange.jpg)'   Note Do not use the &quot;g&quot; flag on the regex as this will create an infinite loop .
  • 19. succ :: String succ() -> string  Used internally by ObjectRange . Converts the last character of the string to the following character in the Unicode alphabet.   Examples 'a' .succ(); // -> 'b' 'aaaa' .succ(); // -> 'aaab' times :: String times(count) -> String   Concatenates the string count times.   &quot;echo &quot; .times( 3 ); //-> &quot;echo echo echo &quot;
  • 20. toArray :: String toArray() -> [character...]  Splits the string character-by-character and returns an array with the result.   Examples   'a' .toArray(); // -> ['a']   'hello world!' .toArray(); // -> ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'] toJSON :: String toJSON() -> String   Returns a JSON string.   'The &quot;Quoted&quot; chronicles' .toJSON(); //-> '&quot;The \&quot;Quoted\&quot; chronicles&quot;'   See also evalJSON
  • 21. toQueryParams :: String toQueryParams([separator = '&']) -> Object   Parses a URI-like query string and returns an object composed of parameter/value pairs .   This method is realy targeted at parsing query strings (hence the default value of &quot;&&quot; for the separator argument).   For this reason, it does not consider anything that is either before a question mark (which signals the beginning of a query string) or beyond the hash symbol (&quot;#&quot;), and runs decodeURIComponent() on each parameter/value pair.   toQueryParams() also aggregates the values of identical keys into an array of values.   Note that parameters which do not have a specified value will be set to undefined. Cont…  
  • 22. toQueryParams :: String …Cont Examples   'section=blog&id=45' .toQueryParams(); // -> {section: 'blog', id: '45'}   'section=blog;id=45' .toQueryParams(); // -> {section: 'blog', id: '45'}   'http://www.example.com?section=blog&id=45#comments' .toQueryParams(); // -> {section: 'blog', id: '45'}   'section=blog&tag=javascript&tag=prototype&tag=doc' .toQueryParams(); // -> {section: 'blog', tag: ['javascript', 'prototype', 'doc']}   'tag=ruby on rails' .toQueryParams(); // -> {tag: 'ruby%20on%20rails'}   'id=45&raw' .toQueryParams(); // -> {id: '45', raw: undefined}
  • 23. truncate :: String each(iterator) -> Hash   Iterates over the name/value pairs in the hash.   Of course, truncate() does not modify strings which are shorter than the specified length.   If unspecified, the length parameter defaults to 30 and the suffix to &quot;...&quot;.   Note that truncate() takes into consideration the length of the appended suffix so as to make the returned string of exactly the specified length.   Examples   'A random sentence whose length exceeds 30 characters.' .truncate(); // -> 'A random sentence whose len...'   'Some random text' .truncate(); // -> 'Some random text.'   'Some random text' .truncate( 10 ); // -> 'Some ra...'   'Some random text' .truncate( 10 ,  ' [...]' ); // -> 'Some [...]'
  • 24. underscore :: String underscore() -> string   Converts a camelized string into a series of words separated by an underscore (&quot;_&quot;).   Example 'borderBottomWidth' .underscore(); // -> 'border_bottom_width'   Note Used in conjunction with dasherize(), underscore() converts a DOM style into its CSS equivalent. 'borderBottomWidth' .underscore().dasherize(); // -> 'border-bottom-width'
  • 25. unescapeHTML :: String unescapeHTML() -> string   Strips tags and converts the entity forms of special HTML characters to their normal form .   Examples   'x &gt; 10' .unescapeHTML() // -> 'x > 10'   '<h1>Pride &amp; Prejudice</h1>' .unescapeHTML() // -> 'Pride & Prejudice'