The regular expression of JS pen question

Source: Internet
Author: User

I. Traditional manipulation of the review string

How to get a numeric character in a string and output it as an array, such as

Dgfhfgh254bhku289fgdhdy675gfh

Output [254,289,675]

Analysis: The loop uses the Charat () method to obtain each substring, judging whether he is between 0~9, and throwing him into the prepared array.

var str= "DGFHFGH254BHKU289FGDHDY675GFH"; FindNum (str); function FindNum () {    var arr=[];    for (Var i=0;i<str.length;i++) {        if (Str.charat (i) <= ' 9 ' &&str.charat (i) >= ' 0 ') {            Arr.push ( Str.charat (i));            Alert (arr);}}    

But this will output [2,5,4,2,8,9,6,7,5] and the result we want a little deviation, so we need a new empty string, each encounter is a number walk if, hit the character go else, go else when the previous numbers are stored in the new empty string, It is then added to the array via push, and then the string is emptied so that it can be stored again

var str= "dgfhfgh254bhku289fgdhdy675"; FindNum (str); function FindNum () {    var arr=[];    var result= "";    for (Var i=0;i<str.length;i++) {        if (Str.charat (i) <= ' 9 ' &&str.charat (i) >= ' 0 ') {            result+= Str.charat (i);        Note that the direction of the addition is not reversed        }        else{            if (result) {                Arr.push (result);                Result= "";}}    }    Alert (ARR)}

There is a hidden danger: If you end up with a number, you will not end up with else, and the last number will not be read, so add the if loop outside of the For loop again

Two. What is a regular, what is the use of

Regular: Also called rules, so that computers can read human rules.

* Which areas of the front end are used in regular?

such as the registration page to enter the user name, we give a set of rules to determine whether he entered the right

Range

Regular is used to manipulate strings (i.e. don't use him to manipulate objects or anything)

* How to write?

Regular is also a system object, like arrays, JSON, there is a rule

abbreviated VAR re=//; Write only two slash, the browser will think is a comment, so try not to give him empty, notice between the two slashes do not have quotation marks

Full name var re=new RegExp (); Reg is a shorthand for regular, exp is shorthand for expressions

Most of the cases are abbreviated, with only one case in full name

Three. Common methods of regular expressions

1.test

Matches the string, if the match succeeds returns true, the match fails to return false

notation: Regular. Test (String)

Such as:

var str= ' abcdef ';

var re=/b/; BC A whole is also in the string, pop true, but write BD, pop false, because there is no BD in the string so a whole

Alert (Re.test (str));

Popup true

Extended:

Detects if a string is full of numbers

var str= ' 8621t56461 ';

var re=//;

if (Re.test (str)) {

Alert ("Not all Numbers");

}

else{

Alert ("All numbers");

}

What do you want to write between the two slashes? Introducing Escape characters

\s (\s): space (non-space)

\d (\d): Digital (not digital)

\w (\w): Character (non-character) characters include letters, numbers, underscores

2.search

3.match

4.replace

The regular expression of JS pen question

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.