GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing en.wikibooks.org — JavaScript/Regular expressions - Wikibooks, open books for an open world OverviewJavaScript implements regular expressions (regex for short) when searching for matches within a string. As with other scripting languages, this allows searching beyond a simple letter-by-letter match, and can even be used to parse strings in a certain format. Unlike strings, regular expressions are delimited by the slash (/) character, and may have some options appended. Regular expressions most commonly appear in conjunction with the string.match() and string.replace() methods. At a glance, by example: strArray = "Hello world!".match(/world/); // Singleton array; note the slashes strArray = "Hello!".match(/l/g); // Matched strings are returned in a string array "abc".match(/a(b)c/)[1] === "b" // Matched subgroup is the 2nd item (index 1) str1 = "Hey there".replace(/Hey/g, "Hello"); str2 = "N/A".replace(/\//g, ","); // Slash is escaped with \ str3 = "Hello".replace(/l/g, "m").replace(/H/g, "L").replace(/o/g, "a"); // Pile if (str3.match(/emma/)) { console.log("Yes"); } if (str3.match("emma")) { console.log("Yes"); } // Quotes work as well "abbc".replace(/(.)\1/g, "$1") === "abc" // Backreference CompatibilityJavaScript's set of regular expressions follows the extended set. While copying a Regex pattern from JavaScript to another location may work as expected, some older programs may not function as expected.
Examples
ModifiersSingle-letter modifiers:
Operators
Function callFor complex operations, a function can process the matched substrings. In the following code, we are capitalizing all the words. It can't be done by a simple replacement, as each letter to capitalize is a different character: var capitalize = function(matchobj) { var group1 = matchobj.replace(/^(\W)[a-zA-Z]+$/g, "$1"); var group2 = matchobj.replace(/^\W([a-zA-Z])[a-zA-Z]+$/g, "$1"); var group3 = matchobj.replace(/^\W[a-zA-Z]([a-zA-Z]+)$/g, "$1"); return group1 + group2.toUpperCase() + group3; }; var classicText = "To be or not to be?"; var changedClassicText = classicText.replace(/\W[a-zA-Z]+/g, capitalize); console.log(changedClassicText==="To Be Or Not To Be?"); The function is called for each substring. Here is the signature of the function: function (''<matchedSubstring>[, <capture1>, ...<captureN>, <indexInText>, <entireText>]'') { ... return ''<stringThatWillReplaceInText>''; }
See also
External links
Hidden category: | ||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |