Using regular expressions in JavaScript

Source: Internet
Author: User

Regular Expression , also known as formal notation , is a method of expressing text patterns. Typically, a single string is used to describe a series of strings that match a rule. Regular expressions are a way of working with strings and returning results, and many programming languages have designed them, and the implementation of regular expressions in JavaScript is based on Perl syntax rules. I remember the teacher who taught me programming said, "The Taoist Exorcism, the doctor's prescription, the programmer's regular expression" is called Three industry miracles, which means that people in non-industry can hardly understand what it means. In fact, the regular expression is still easy to get started, but to master the words still need to talk some time.

Now there is a requirement that there is a string that we need to pick out all the numbers in the string and save them in the array, without regular expressions we can consider using JavaScript's string function to implement the code as follows:

 var  str = "ash1kj27ja875jsow5096hikjhs1pols65787"  var  arr = [];   var  temp = ""; //  temporary save variable  for  (var  i=0;i<str.length;i++) { if  (str[i]<= "9" && str[i]>= "0"  +=str[i];  else  {temp  && Arr.push (temp);    Temp  = "" ; }}temp  && Arr.push (temp); Console.log (arr);  

The results of the implementation are as follows:

Strings can be compared, so we can find out all the numbers with only numbers that are larger than "0" and less than "9". The disadvantages of using the string method to operate are the following two:

1, the operation is cumbersome, if the need to carry out the operation is very complex, it needs a better algorithm design to complete.

2, the efficiency is very low, the continuous execution cycle operation very affects the program performance.

The use of regular expressions to manipulate the words is much simpler, the code is as follows:

var str = "ash1kj27ja875jsow5096hikjhs1pols65787"; Console.log (Str.match (/\d+/g));

Using regular expressions two lines of code can achieve the same effect, and the execution is very high, so still feel that the regular expression is really a weapon of God, the following we are slowly speaking of JavaScript in the regular expression of those things.

Using regular expressions in JavaScript

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.