Computer to pick up from the doll---------Regular expression (study note i)

Source: Internet
Author: User

This article is mainly to the regular, learning new things, I remember the teacher said a good way, to take questions to learn, not light to see the theory, you have to always think of this thing is what, how to use, where, I think the said is very good. So always think about these questions when you look at them.

This is the meow of the study notes may have a lot of shortcomings, I hope you can more comments!

Let's go, Class!

What is a regular expression?

Regular: Also called rules, can let the computer read the rules of Human.

(Note: The regular are all manipulation strings)

A regular notation:

var re =/a/; Write two here//the system will think this is a comment, so it's best to write something.

var re = new RegExp (' a ');

The default is case-sensitive in regular, if not case-sensitive, at the end of the regular ID I

Regular default: The regular match succeeds and the match is not continued. If you want to find all you need to add the identity G (Global match)

Common methods:

1)

Test: Regular to match string, if the match succeeds returns true, the failure returns false.

The notation for test: Regular. Test (string);

eg

var str = ' abcddef ';

var re =/b/;

Alert (Re.test (str));

This will return ture, because the matching B is required in the string;

What if the matching BC? (will also return ture)

What if the matching BD? (Will return FALSE, because BD is not a whole)

Escape characters:

\s: Space

\s: Non-whitespace

\d: Digital

\d; non-digital

\w: Characters (Letters, numbers, underscores)

\w: Non-self-character

2)

Search: Matches the string, if the match succeeds, returns the successful position, and returns 1 if the match fails.

The wording of search: string. Search (regular);

eg

var str = ' abcdef ';

var re =/b/;

Alert (Str.search (re));

Apparently found it. So return-1;

If you write like this

var re =/b/

Run again

Str.search (re);

Return is-1, you can see that the default is case-sensitive in the regular;

3)

Match: Matches the string, if the match succeeds, returns an array of successful matches, and returns null if the match is unsuccessful;

Match's notation: string. Match (regular);

eg

var str = ' Jdl46d4a6sd464sd4w4a4f6 ';

var re =/\d/;

Alert (Str.match (re)); [4];

There is only one [1] followed by a lot of numbers that are not added to the array, why?

This is the default behavior of the regular

Regular default: The regular match succeeds and the match is not continued. If you want to find all you need to add the identity G (Global match)

This will be written in the following form:

var re =/\d/g;

var str = ' Jdl46d4a6sd464sd4w4a4f6 ';

var re =/\d/g;

Alert (Str.match (re)); [4,6,4,6,4,6,4,4,4,4,6];

Each of these is added to the array separately, and if I want to make a group of sequential numbers, how can i solve them?

In general, you may think

var re =/\d\d/g;

Write like this

If the number is not certain, so write ... Have flaws

This is the time to use the quantifier (my God, so magical?)

Quantifier: Match an indeterminate position

+: appears at least once

var re =/\d+/g; That means the number appears at least once.

4)

Replace: Regular to match string, match successful character to replace with new string

How replace is written: string. Replace (regular, new string)

eg

var str = ' AAA ';

var a =/a/;

str = str.replace (Re, ' B ');

alert (str);

The result is ' BAA ';

Because the program matches to the first a after the first A is replaced with B; What if I want all a to be replaced by B? What should I do?

var re =/a/g;

So all A is replaced by a B? You can try it yourself can rely on the knowledge of the previous realization;

I will write some actual chestnuts in the back, although most of them are carried. Because it is a study note, I will do some original article although not necessarily the technology

Computer to pick up from the doll---------Regular expression (study note i)

Related Article

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.