Javascript search

search(regexp)
The search() method is the same as indexOf() except that it takes a
regular expression pattern instead of a substring. It also returns -1 if the
pattern isn’t found.
"hello world".search(/[aeiou]/); // Find the fi rst vowel
1
"hello world".search(/\d/); // Find the fi rst digit
-1
match(regexp)
The match() method returns an array containing all the substrings
matching the regular expression and its subpatterns. Unlike the other
search-and-replace methods, it returns null if the pattern doesn’t match.
Here are some simple examples, but I’ll cover this function more in
Chapter 8:
// Find all the vowels
"hello world".match(/[aeiou]/g);
["e", "o", "o"]
// Find "world" regardless of capitalization
"hElLo WoRlD".match(/world/i);
["WoRlD"]
replace(pattern, replacement)
The replace() method works like match() except that it returns a string
with all instances of pattern replaced by the string replacement.
// Remove all non-numeric characters from a phone number
"(310) 555-9876".replace(/\D/g, "");
Related Posts:
  • Expression Statements Assignment statements are one major category ofexpression statements.  For example: greeting = "Hello " + name;i *= 3; The increment and decrement operators, ++ and --, are relatedto assignment statements. These have … Read More
  • Remote JavaScript The primary reason is that the round trip time required to submit a form and then download the response is often inconvenient. The user experience is much improved if, instead of clicking a Submit button and watching the … Read More
  • concat() Method The concat() method returns the array resulting from appending its arguments to  the array on which it was invoked. Given the script: var myArray = ["red", "green", "blue"]; alert(myArray.concat("cyan", "yello… Read More
  • Creating Elements, Attributes, and Objects An important benefit of dynamic content is the ability to create new content from freshly received data. This is necessary in dynamic menu creation, navigation, breadcrumbs, and web services, among other applications. Ajax… Read More
  • The try-catch Statement JavaScript provides a try-catch statement that is capable of intercepting thrown errors before they are handled by the browser. The code that might cause an error comes in the try block and code that handles the error goes i… Read More
  • Reserved Words in JavaScript Reserved Words in JavaScript 1.5 >abstract else instanceof switch >boolean enum int synchronized >break export interface this byte extends long throw case f… Read More
  • JavaScript Dynamic Links and Menus Many sites use JavaScript to create links to other website pages. Here is some examplecode with different link types that you may want to avoid: <HTML><head><title>Link Examples ~ Things to stay away from&l… Read More
  • HTTP “200 OK” response A complete request is not necessarily a successful request, andyour handler for the load event should check the status codeof the XMLHttpRequest object to ensure that you received anHTTP “200 OK” response rather than a “404 … Read More
  • Embedding Content for Plug-Ins Although never officially a part of any HTML specification, the <<embed>> tag is most often used to include embedded objects for Netscape and Internet Explorer. A Macromedia Flash file might be embedded as fo… Read More
  • var keyword JavaScript The var and function are declaration statements—they declare or define variables and functions. These statements defineidentifiers (variable and function names) that can be used elsewherein your program and assign values to … Read More
  • JavaScript Object Literals Object literals are a popular way to create new objects with a specific set of properties, as opposed to explicitly creating a new instance of Object and then adding properties. For example, this pattern is rarely used: … Read More
  • Function Call Spacing Almost universally, the recommended style for function calls is to have no space between the function name and the opening parenthesis, which is done to differentiate it from a block statement. For example: // Good doSom… Read More
  • JavaScript Form Validation  JavaScript Form Validation. JavaScript can be used to validate data in HTML forms <script type="text/javascript"> function validate_all()     {     var frmReg=document.manageadmin… Read More
  • JavaScript Security Downloading and running programs written by unknown parties is a dangerous proposition. A program available on the Web could work as advertised, but then again it could also install spyware, a backdoor into your system, … Read More
  • JavaScript Refresh Page you refresh the page using document.location.reload(). You can add the true keyword to force the reloaded page to come from the server (instead of cache). Alternatively, you can use the false keyword to reload the page from … Read More