JS regular expression, error handling and debugging

Source: Internet
Author: User
Tags button type string methods

Regular Expressions (English: Regular expression, often abbreviated in code as regex, RegExp, or re) use a single string to describe and match a series of string search patterns that conform to a certain syntactic rule.

Grammar:

/pattern/modifiers;

Pattern (expression, meta-character, quantifier) is represented by patterns. For example: [Abc],[0-9],\d,\s,\b,\uxxxx,n+,n*,n?.

Modifiers is a modifier. I is not sensitive to case, G represents global match; M performs multi-line matching.

Regular expressions are typically used for two string methods: Search () and replace ().

The Search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression and returns the starting position of the substring.

The Replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

The test () method is used to detect whether a string matches a pattern and returns true if the string contains matching text, otherwise false.

The exec () method is used to retrieve the matching of regular expressions in a string. The function returns an array that holds the results of the match. If no match is found, the return value is null.

The TRY statement tests the code block for errors, the catch statement handles the error, and the throw statement creates a custom error.

<! DOCTYPE html>functionmyFunction () {Try{ varX=document.getelementbyid ("Demo"). Value;if(x== "")Throw"Value is empty";if(IsNaN (x))Throw"Not numbers.";if(X&GT;10)Throw"Too Big";if(x<5)Throw"Too small";}Catch(err) {varY=document.getelementbyid ("mess"); y.innerhtml= "Error:" + Err + ". ";}}</script>
 
  
 

It is difficult to write JavaScript programs without Debugging tools. Looking for errors in program code is called Code debugging. Many browsers have built-in debugging tools.

With the debugging tools, we can set breakpoints (where the code stops executing), and you can detect variables as the code executes.

The browser-enabled debugging tool typically presses the F12 key and selects "Console" in the Debug menu.

If your browser supports debugging, you can use the Console.log () method to print JavaScript values on the debug window:

<!DOCTYPE HTML><HTML><Body><H1>My first Web page</H1><P>In the browser (Chrome, IE, Firefox) use F12 to enable debug mode, click the "Console" menu in the Debug window. </P><Script>a= 5; b= 6; c=a+B;console.log (c);</Script></Body></HTML>         

In the Debug window, you can set breakpoints for JavaScript code.

The debugger keyword is used to stop executing JavaScript and invoke the Debug function.

JS regular expression, error handling and debugging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.